Field Value in Query Based on Other Fields (1 Viewer)

MaryD

New member
Local time
Today, 08:33
Joined
Sep 18, 2023
Messages
2
Hi all

It's been a while since I used Access and I'm currently tying myself in knots with a query on data imported from another system. I can code a little, but in this case I'm trying to just use the query builder and can't quite get the results I want.

Simplified version - Fields are PersonId, StartDate and ApplicationId (unique).

The personId can be in the table multiple times. I'm trying to find the person's latest start date and the corresponding ApplicationID. So I'm grouping by personId and selecting Max for StartDate, but I'm struggling to get it to return the applicationID for that date.

Person IdStart DateApplication Id
12341st April 2023121212
12345th May 2023678987
123411th October 2022145687
Group ByMax?????

And I want it to return

12345th May 2023678987

Hope that makes sense - any an all input much appreciated!!!!

Mary
 

June7

AWF VIP
Local time
Yesterday, 23:33
Joined
Mar 9, 2014
Messages
5,472
One way is with a correlated subquery using TOP N. Review http://allenbrowne.com/subquery-01.html#TopN

Another is to JOIN table to aggregate query that returns Max(StartDate) for each person.

SELECT table.*, MDTE FROM table INNER JOIN (SELECT PersonID, Max(StartDate) AS MDTE FROM table) AS Q ON table.PersonID = Q.PersonID AND table.StartDate = Q.MDTE;

You show dates formatted with ordinals - dates need to be actual dates in a date/time field, not text.
 

MaryD

New member
Local time
Today, 08:33
Joined
Sep 18, 2023
Messages
2
Thanks June7, dates are formatted as dates, listed them as ordinals to rule out any confusion with US/European date format. I'll give it a go, Access skills very rusty but I'm starting to remember how much I enjoyed it!!
 

Users who are viewing this thread

Top Bottom