Get only latest row, grouped by a column

后端 未结 5 1462
醉酒成梦
醉酒成梦 2021-01-23 03:27

I have a large data-set of emails sent and status-codes.

ID Recipient           Date       Status
 1 someone@example.com 01/01/2010      1
 2 someone@example.com         


        
5条回答
  •  猫巷女王i
    2021-01-23 03:53

    You cannot easily do this is a single query because count(*) is a group function whereas the latest status comes from a specific row. Here is the query to get the latest status for each user:

    SELECT M.Recipient, M.Status FROM Messages M
    WHERE M.Date = (SELECT MAX(SUB.Date) FROM MESSAGES SUB
        WHERE SUB.Recipient = M.Recipient)
    

提交回复
热议问题