简体   繁体   中英

select a column corresponding to max value in two joined tables

I have two tables, say Users and Interviews. One user can have multiple interview records.

Users
-----
UserID
FirstName
LastName

Interviews
----------
InterviewID
UserID
DateOfInterview

I want to get only the latest interview records. Here's my query

select u.UserID, firstname, lastname, max(DateOfInterview) as latestDOI 
from users u 
left join interviews i 
on u.UserID = i.UserID 
GROUP BY u.UserID, firstname, lastname
ORDER BY max(DateOfInterview) DESC

How do I update the query to return the InterviewID as well (ie the one which corresponds to max(DateOfInterview))?

Instead of using an aggregate function in your select list, you can use an aggregate subquery in your WHERE clause:

select u.UserID, firstname, lastname, i.InterviewId, DateOfInterview as latestDOI 
from users u 
left join interviews i 
  on u.UserID = i.UserID 
where i.UserId is null or i.DateOfInterview = (
  select max(DateOfInterview)
  from interviews i2
  where i2.UserId = u.UserId
)

That does suppose that max(DateOfInterview) will be unique per user, but the question has no well-defined answer otherwise. Note that the main query is no longer an aggregate query, so the constraints of such queries do not apply.

There are other ways to approach the problem, and it is worthwhile to look into them because a correlated subquery such as I present can be a performance concern. For example, you could use an inline view to generate a table of the per-user latest interview dates, and use joins to that view to connect users with the ID of their latest interview:

select u.*, im.latestDOI, i2.InterviewId
from
  users u
  left join (
      select UserID, max(DateOfInterview) as latestDOI 
      from interviews i
      group by UserID
    ) im
    on u.UserId = im.UserId
  left join interviews i2
    on im.UserId = i2.UserId and im.latestDOI = i2.DateOfInterview

There are other alternatives, too, some standard and others DB-specific.

Rewrite to use an OUTER APPLY when grabbing your interview, that way you can use order by rather than MAX

select u.UserID, firstname, lastname, LatestInterviewDetails.ID, LatestInterviewDetails.DateOfInterview as latestDOI 
from users u 
OUTER APPLY (SELECT TOP 1 Id, DateOfInterview
    FROM interviews
    WHERE interviews.UserID = u.UserId
    ORDER BY interviews.DateOfInterview DESC
) as LatestInterviewDetails

Note: This is providing you are using Microsoft SQL Server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM