Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Based on the change history sample table below, how do I query to get the latest revision only (where Rev is highest) for each unique FldID? i.e. I would like to select records with ID = 003 and 005ID Rev Data FldID-- --- ---- ---01 001 Joes 00102 002 Jona 00103 003 Jobs 00104 001 Pets 00205 002 Pats 002thanks
rohitkumar
Constraint Violating Yak Guru
472 Posts
Posted - 2008-10-15 : 14:55:52
create a sub-query with max(rev) group by FldID and join it with your select statement on this table.
acesover
Starting Member
15 Posts
Posted - 2008-10-15 : 19:44:50
That worked. Thanks.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-10-16 : 03:02:22
or use ROW_NUMBER() in sql 2005
SELECT ID ,Rev, Data, FldIDFROM(SELECT ROW_NUMBER() OVER(PARTITION BY FldID ORDER BY Rev DESC) AS Seq,*FROM YourTable)tWHERE Seq=1