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
You can use the ranking functions for this. Something like (not tested):
WITH MyResults AS
(
SELECT Recipient, Status, ROW_NUMBER() OVER( Recipient ORDER BY ( [date] DESC ) ) AS [row_number]
FROM Messages
)
SELECT MyResults.Recipient, MyCounts.EmailCount, MyResults.Status
FROM (
SELECT Recipient, Count(*) EmailCount
FROM Messages
GROUP BY Recipient
) MyCounts
INNER JOIN MyResults
ON MyCounts.Recipient = MyResults.Recipient
WHERE MyResults.[row_number] = 1