1. 程式人生 > 其它 >各科成績排序,不分科目進行成績排序

各科成績排序,不分科目進行成績排序

技術標籤:SQL server 筆記sql資料庫sqlserver

資料,建立表,檢視資料表(程式碼)~

1、按成績(不分科目)進行排序,並顯示排名,Score 重複時保留名次空缺

select *,RANK()over(order by score desc)名次 from SC 


在這裡插入圖片描述

2、按成績(不分科目)進行排序,並顯示排名,Score 重複時合併名次(並列第…)

select *,DENSE_RANK()over(order by score desc)名次 from SC


在這裡插入圖片描述

3、按各科成績(分科目)進行排序,並顯示排名,Score 重複時保留名次空缺

--ways 1
select * , (select count(score)+1 from SC B where B.C# = A.C# and B.score > A.score) 名次 from SC A order by C#,名次
--ways 2
select *,RANK()over(order by score desc)名次 from SC where C# ='01' union all
select *,RANK()over(order by score desc)名次 from SC where C# ='02' union all
select *,RANK()over(order
by score desc)名次 from SC where C# ='03'


在這裡插入圖片描述

3、按各科成績(分科目)進行排序,並顯示排名,重複時合併名次(並列第…)

--ways 1
select * ,
(select count(distinct score)+1 from 
SC B where B.C# = A.C# and B.score > A.score) 名次 from 
SC A order by C#,名次
--ways 2
select *,DENSE_RANK()over(order by score desc)名次 from 
SC where C# ='01' union all
select *,DENSE_RANK()over(order by score desc)名次 from SC where C# ='02' union all select *,DENSE_RANK()over(order by score desc)名次 from SC where C# ='03'


在這裡插入圖片描述