SQL 排序的N种方法
一、手工查询得出名次
select * ,select count(*)+1 from T2 where T2.[成绩] > T1.[成绩] as 名次 from T1
结果:
1
2
3
3
5
二、SQL Server 2005 的排序函数 ROW_NUMBER、RANK、DENSE_RANK
1.ROW_NUMBER
select *,ROW_NUMBER() over(partition by 分组列1,分组列2 ORDER BY 排序1,排序2) from T
结果:
1
2
3
4
5
2.RANK
select *,RANK() over(partition by 分组列1,分组列2 ORDER BY 排序1,排序2) from T
结果:
1
2
3
3
5
3.DENSE_RANK
select *,DENSE_RANK() over(partition by 分组列1,分组列2 ORDER BY 排序1,排序2) from T
结果:
1
2
3
3
4