Present the following schema and sample data (SQL Server 2008):
OriginatingObject
----------------------------------------------
ID
1
2
3
ValueSet
----------------------------------------------
ID OriginatingObjectID DateStamp
1 1 2009-05-21 10:41:43
2 1 2009-05-22 12:11:51
3 1 2009-05-22 12:13:25
4 2 2009-05-21 10:42:40
5 2 2009-05-20 02:21:34
6 1 2009-05-21 23:41:43
7 3 2009-05-26 14:56:01
Value
----------------------------------------------
ID ValueSetID Value
1 1 28
etc (a set of rows for each related ValueSet)
I need to get the identifier of the most recent ValueSet for each OriginatingObject. Do not assume that the higher the record identifier, the later it is.
I'm not sure how to correctly use GROUP BY to make sure that the result set, grouped together to form each aggregate row, contains the row identifier with the highest DateStamp for this grouping. Do I need to use a subquery or is there a better way?
source
share