Give me top n comments for all my entities please
Wednesday, February 10, 2010 by Administrator
A quick TSQL Nugget.
I recently needed to pull from the database a list of top 3
comments for all "objects"
Using the TSQL ROW_NUMBER() function with PARTITION makes this
easy!
SELECT
ObjectID, CommentID, ROW_NUMBER() OVER ( PARTITION BY ObjectID ORDER BY CommentID DESC ) AS RowNumber
FROM
dbo.Comment
Want to Leave a comment?