当前位置: 代码迷 >> Sql Server >> sql server 对成绩的排序解决方案
  详细解决方案

sql server 对成绩的排序解决方案

热度:51   发布时间:2016-04-27 11:34:26.0
sql server 对成绩的排序
把一个班的成绩分为A、B、C三个等级前十名为A,后十名为C,中间为B
求指教~~~

------解决方案--------------------
大致如下:

with temp as
(
select ID, 成绩,名次=ROW_NUMBER()OVER(PARTITION BY 成绩 ORDER BY 成绩 desc) 
FROM 表格
)
select ID, 成绩, 等级 = (case when 名次 between 1 and 10 then 'A'
else 'B')
from temp
------解决方案--------------------
--A table
select top 10 * from tb 
order by record 
 
--B table
select * from tb
where 
id not in(select top 10 id from tb 
order by record )
and id not in(
select top 10 id from tb 
order by record desc
 )
--C table
select top 10 * from tb 
order by record desc
 
------解决方案--------------------
借用了下1L的代码,做了一些补充
SQL code
with temp as(  select ID, 成绩,名次=ROW_NUMBER()OVER(PARTITION BY 成绩 ORDER BY 成绩 desc)    FROM 表格)declare @count intselect @count=count(1) from 表格select ID, 成绩, 等级 = (case when 名次 between 1 and 10 then 'A'                              when 名次 between @count-9 and @count then 'C'  else 'B')from temp
  相关解决方案