Solved Top values for unique records (2 Viewers)

mounty76

Registered User.
Local time
Today, 05:14
Joined
Sep 14, 2017
Messages
341
Hi All,

I have a table which gets joining and leaving dates for crew, it is something like this:

SeatimeID, CrewID, JoinDate, LeaveDate

The CrewID is linked to a crew table and so therefore the data in the seatime table looks something like this :

1, 120, 1/1/23, 2/1/23
2, 12, 12/1/23, 23/4/23
3, 110, 12/2/22, 23/6/22
4, 120, 4/4/23, 17/6/23
5, 12, 6/8/23, 9/10/23

My question....how do I find the highest SeatimeID for each particular CrewID? I really want the query to return

3, 110, 12/2/22, 23/6/22
4, 120, 4/4/23, 17/6/23
5, 12, 6/8/23, 9/10/23

I've tried a few different ways to achieve this and I cannot seem to get it right?
 

RogerCooper

Registered User.
Local time
Today, 05:14
Joined
Jul 30, 2014
Messages
286
You will need 2 queries. The first query selects the max Seatime_ID for each Crew_ID. The second query links the first query to the original data to pull up the dates.

Queries referencing other queries is a useful technique.
 

ebs17

Well-known member
Local time
Today, 14:14
Joined
Feb 7, 2020
Messages
1,946
Recognize the analogy:
 

bob fitz

AWF VIP
Local time
Today, 13:14
Joined
May 23, 2011
Messages
4,727
Try:
Code:
SELECT Max(YourTableName.SeatimeID) AS MaxOfSeatimeID, YourTableName.CrewID, Last(YourTableName.JoinDate) AS LastOfJoinDate, Last(YourTableName.LeaveDate) AS LastOfLeaveDate
FROM YourTableName
GROUP BY YourTableName.CrewID
ORDER BY Max(YourTableName.SeatimeID);
 

Users who are viewing this thread

Top Bottom