Secret Orange Web Log

Give me top n comments for all my entities please

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

0 comment(s)

Want to Leave a comment?

Name:  
Email:  
Website:
Comment: