日期:2014-05-18  浏览次数:20547 次

不同学科同时排班名次和校名次问题
原始数据如下:

班 姓名 语文 数学 总分
1 张三 80 81
1 张四 81 79
1 王五 79 66
2 钱七 66 89
2 孙八 89 77
2 周九 77 53
3 吴十 53 91
3 钱二 91 42
3 杨一 42 74

现在想对各科作如下排序,包括计算总分
班 姓名 语文 班名次 校名次 数学 班名次 校名次 总分 班名次 校名次
1 张三 80 81
1 张四 81 79
1 王五 79 66
2 钱七 66 89
2 孙八 89 77
2 周九 77 53
3 吴十 53 91
3 钱二 91 42
3 杨一 42 74

求高手给出SQL语句学习下

------解决方案--------------------
SQL code

declare @T table (班 int,姓名 varchar(4),语文 int,数学 int,总分 sql_variant)
insert into @T
select 1,'张三',80,81,null union all
select 1,'张四',81,79,null union all
select 1,'王五',79,66,null union all
select 2,'钱七',66,89,null union all
select 2,'孙八',89,77,null union all
select 2,'周九',77,53,null union all
select 3,'吴十',53,91,null union all
select 3,'钱二',91,42,null union all
select 3,'杨一',42,74,null

select 
班,姓名,语文,
班名次=(select count(1) from @T where 班=t.班 and 语文>=t.语文),
校名次=(select count(1) from @T where 语文>=t.语文),
数学,
班名次=(select count(1) from @T where 班=t.班 and 数学>=t.数学),
校名次=(select count(1) from @T where 数学>=t.数学),
总分=语文+数学,
班名次=(select count(1) from @T where 班=t.班 and 语文+数学>=t.语文+t.数学),
校名次=(select count(1) from @T where 语文+数学>=t.语文+t.数学)
from @T t

/*
班           姓名   语文          班名次         校名次         数学          班名次         校名次         总分          班名次         校名次
----------- ---- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1           张三   80          2           4           81          1           3           161         1           2
1           张四   81          1           3           79          2           4           160         2           3
1           王五   79          3           5           66          3           7           145         3           5
2           钱七   66          3           7           89          1           2           155         2           4
2           孙八   89          1           2           77          2           5           166         1           1
2           周九   77          2           6           53          3           8           130         3           8
3           吴十   53          2           8           91          1           1           144         1           6
3           钱二   91          1           1           42          3           9           133         2           7
3           杨一   42          3           9           74          2           6           116         3           9
*/

------解决方案--------------------
SQL code

create table yxdxcy
(班 int, 姓名 char(6), 语文 int, 数学 int, 总分 int)

insert into yxdxcy(班,姓名,语文,数学)
select 1, '张三', 80, 81 union all 
select 1, '张四', 81, 79 union all  
select 1, '王五', 79, 66 union all  
select 2, '钱七', 66, 89 union all  
select 2, '孙八', 89, 77 union all  
select 2, '周九', 77, 53 union all  
select 3, '吴十', 53, 91 union all  
select 3, '钱二', 91, 42 union all  
select 3, '杨一', 42, 74 


select a.班, a.姓名, 
a.语文, b.rn '班名次', c.rn '校名次',
a.数学, d.rn '班名次', e.rn '校名次',
a.语文+a.数学 '总分', f.rn '班名次', g.rn '校名次'
from yxdxcy a
inner join
(select 班,姓名,row_number() over(partition by 班 order by 语文 desc) rn from yxdxcy) b
on a.班=b.班 and a.姓名=b.姓名
inner join
(select 班,姓名,row_number() over(order by 语文 desc) rn from yxdxcy) c
on a.班=c.班 and a.姓名=c.姓名
inner join
(select 班,姓名,row_number() over(partition by 班 order by 数学 desc) rn from yxdxcy) d
on a.班=d.班 and a.姓名=d.姓名
inner join
(select 班,姓名,row_number() over(order by 数学 desc) rn from yxdxcy) e
on a.班=e.班 and a.姓名=e.姓名
inner join
(select 班,姓名,row_number() over(partition by 班 order by 语文+数学 desc) rn from yxdxcy) f
on a.班=f.班 and a.姓名=f.姓名
inner join
(select 班,姓名,row_number() over(order by 语文+数学 desc) rn from yxdxcy) g
on a.班=g.班 and a.姓名=g.姓名


班           姓名     语文