在一个贴子中看到了,但是排名一般有两种需要,一个是不间断排,一个是间断排。下面只是一个小例子,求班级排名和年级排名。
因2005增加了分析函数部分(至少在oracle中这样称呼,我是半路出家,转来弄MSSQL)应该可以很容易的实现了。
下面只说说2000的怎么处理。如果有不足的地方希望各位修正!
怎么没有修改回复的功能呢?关于排序,不间断排序,如两个第二,接下来还是第三
--> 测试数据: @t
declare @t table (姓名 varchar(1),班级 int,语文 int,数学 int,物理 int)
insert into @t
select 'A',1,98,56,23 union all
select 'B',1,89,68,46 union all
select 'G',1,89,68,46 union all
select 'C',1,65,74,65 union all
select 'D',2,69,87,74 union all
select 'F',2,69,87,74 union all
select 'E',2,45,56,58select *,总分=语文+数学+物理,
班级名次=(select count(distinct 语文+数学+物理)  from @t where 班级=a.班级 and 语文+数学+物理>=a.语文+a.数学+a.物理),
年级名次=(select count(distinct 语文+数学+物理) from @t where 语文+数学+物理>=a.语文+a.数学+a.物理)
from @t aA 1 98 56 23 177 3 4
B 1 89 68 46 203 2 3
G 1 89 68 46 203 2 3
C 1 65 74 65 204 1 2
D 2 69 87 74 230 1 1
F 2 69 87 74 230 1 1
E 2 45 56 58 159 2 5
排序间断,如两个第二,接下来是第四--> 测试数据: @t
declare @t table (姓名 varchar(1),班级 int,语文 int,数学 int,物理 int)
insert into @t
select 'A',1,98,56,23 union all
select 'B',1,89,68,46 union all
select 'G',1,89,68,46 union all
select 'C',1,65,74,65 union all
select 'D',2,69,87,74 union all
select 'F',2,69,87,74 union all
select 'E',2,45,56,58select *,总分=语文+数学+物理,
班级名次=(select count(1)+1 from @t where 班级=a.班级 and 语文+数学+物理>a.语文+a.数学+a.物理),
年级名次=(select count(1)+1 from @t where 语文+数学+物理>a.语文+a.数学+a.物理)
from @t aA 1 98 56 23 177 4 6
B 1 89 68 46 203 2 4
G 1 89 68 46 203 2 4
C 1 65 74 65 204 1 3
D 2 69 87 74 230 1 1
F 2 69 87 74 230 1 1
E 2 45 56 58 159 3 7