sqlserver巧用row_number和partitionby分组取top数据(教程)
程序员文章站
2022-04-23 12:06:08
语法形式:ROW_NUMBER() OVER(PARTITION BY COL1 ORDER BY COL2)
解释:根据COL1分组,在分组内部根据 COL2排序,而此函数计算...
语法形式:ROW_NUMBER() OVER(PARTITION BY COL1 ORDER BY COL2)
解释:根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)
select * from ( select subject,name,score,ROW_NUMBER() over(PARTITION by subject order by score desc) as num from #score ) T where T.num <= 3 order by subject
高级用法