Get only latest row, grouped by a column

后端 未结 5 1457
醉酒成梦
醉酒成梦 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条回答
  •  后悔当初
    2021-01-23 03:58

    It's not very pretty, but I'd probably just use a couple of subselects:

    SELECT Recipient,
        COUNT(*) EmailCount,
        (SELECT Status
         FROM Messages M2
         WHERE Recipient = M.Recipient
             AND Date = (SELECT MAX(Date)
                         FROM Messages
                         WHERE Recipient = M2.Recipient))
    FROM Messages M
    GROUP BY Recipient
    ORDER BY Recipient
    

提交回复
热议问题