WITH Table AS ( SELECT Field1, Field2, Rank() OVER (PARTITION BY Field2 ORDER BY Field1 desc) AS Rank FROM dbo.TableName ) SELECT * from Table WHERE Rank = 1 ;Hope this helps.
I started keeping notes on things I used a lot, or that took care of a difficult problem. On looking for something I couldn't remember how to do, and spending 30 minutes going through notes and SSIS projects, I decided to create this blog. Lots of SQL, BI, Web, Winforms, Spotifre, R, Python and whatever I happen to be working in when I decide to post something I find interesting.
Wednesday, 5 December 2012
Rank and pick best record SQL
I used to use loops for this kind of thing. This is far more efficient.
Labels:
Cursor,
Cursor Alternative,
Loop,
Rank,
Sequence,
Sequence Number,
SQL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment