由于成绩可以并列,所以前五名可能有多个。例如:
测试数据:
declare @t table(ID int, StudentName nvarchar(15), Score int) insert into @tselect 1,"黄一",99 union allselect 2,"吴二",99 union allselect 3,"张三",99 union allselect 4,"李四",98 union allselect 5,"王五",97 union allselect 6,"赵六",96 union allselect 7,"田七",95 union allselect 8,"纪八",94 union allselect 9,"邱九",93 union allselect 10,"林十",92二、自己实现
select t1.* from @t t1join(select distinct top 5 Score from @t order by Score desc) t2on t1.Score = t2.Score看起来和上面的要求的结果还是不太一样,少了排序,当然我们可以在程序处理,这不是问题。
;with cte as(select dense_rank() over(order by Score desc) rank,* from @t)select * from cte where rank < 6四、扩展,内置排名函数RANK
以上就是sql server排名函数DENSE_RANK的使用方法,分享了自己的一些想法,希望对大家的学习有所启发。