读书人

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

发布时间: 2012-09-10 22:20:13 作者: rapoo

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 

读书人网 >SQL Server

热点推荐