Wednesday, 5 June 2013

SQL: Get aggregates for multiple values from the same table

Table Doc

ID Version
--  --
1  1
1  2
1  3*
2  1
2  2*
3  1
3  2*
4  1
4  2
4  3
4  4*
5  1*

To get maximum versions for each document

SELECT D.ID, D.Version
FROM Doc D 
INNER JOIN (SELECT ID, MAX(Version) FROM Doc GROUP BY ID) DM ON D.ID = DM.ID



Thanks Dinos for the inspiration!



1 comment: