Select top 10 records for each category

前端 未结 14 1450
别那么骄傲
别那么骄傲 2020-11-22 04:27

I want to return top 10 records from each section in one query. Can anyone help with how to do it? Section is one of the columns in the table.

Database is SQL Serve

14条回答
  •  半阙折子戏
    2020-11-22 05:25

    If you are using SQL 2005 you can do something like this...

    SELECT rs.Field1,rs.Field2 
        FROM (
            SELECT Field1,Field2, Rank() 
              over (Partition BY Section
                    ORDER BY RankCriteria DESC ) AS Rank
            FROM table
            ) rs WHERE Rank <= 10
    

    If your RankCriteria has ties then you may return more than 10 rows and Matt's solution may be better for you.

提交回复
热议问题