日期:2014-05-17  浏览次数:20798 次

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 int
select @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