Select top 10 records for each category

前端 未结 14 1436
别那么骄傲
别那么骄傲 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:11

    Tried the following and it worked with ties too.

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

提交回复
热议问题