+ -
当前位置:首页 → 问答吧 → 4. 列出“s-01”号课成绩比“s-02”号课成绩高的所有学生的学号

4. 列出“s-01”号课成绩比“s-02”号课成绩高的所有学生的学号

时间:2011-11-19

来源:互联网

学号 课程号 成绩
1001 s-01 80
1001 s-02 70
1001 s-03 60
1001 s-04 50
1001 s-05 40
1002 s-01 55
1002 s-02 77
1002 s-03 68
1002 s-04 50
1002 s-05 89
1003 s-01 80
1003 s-02 35
1003 s-03 76
1003 s-04 89
1003 s-05 93

作者: a_tuzi   发布时间: 2011-11-19

SQL code
--> --> (Roy)生成測試數據
 
if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([学号] int,[课程号] nvarchar(4),[成绩] int)
Insert #T
select 1001,N's-01',80 union all
select 1001,N's-02',70 union all
select 1001,N's-03',60 union all
select 1001,N's-04',50 union all
select 1001,N's-05',40 union all
select 1002,N's-01',55 union all
select 1002,N's-02',77 union all
select 1002,N's-03',68 union all
select 1002,N's-04',50 union all
select 1002,N's-05',89 union all
select 1003,N's-01',80 union all
select 1003,N's-02',35 union all
select 1003,N's-03',76 union all
select 1003,N's-04',89 union all
select 1003,N's-05',93
Go
Select * 
from #T as a
where [成绩]>
(select min([成绩]) from #T where [课程号]='s-02') and [课程号]='s-01'
/*
学号    课程号    成绩
1001    s-01    80
1002    s-01    55
1003    s-01    80
*/

作者: roy_88   发布时间: 2011-11-19

SQL code
Select * 
from #T as a
where [成绩]>
(select max([成绩]) from #T where [课程号]='s-02') and [课程号]='s-01'
/*
学号    课程号    成绩
1001    s-01    80
1003    s-01    80
*/

作者: roy_88   发布时间: 2011-11-19

- -貌似 你的这个方式不可以啊。。 max是干嘛 最大值啊 我要的是所有的学生啊 
而且我这个每个人都要显示啊。。。
不分组求出来的最大值 是所有成绩中的最大值吧

作者: a_tuzi   发布时间: 2011-11-19

select m.学号
from tb m left join tb n
on m.课程号 = 's-01' and n.课程号 = 's-02' and m.学号 = n.学号 and m.成绩 > isnull(n.成绩,0)

作者: dawugui   发布时间: 2011-11-19

此帖中有你这个内容的详细写法:
一个项目涉及到的50个Sql语句(整理版)
http://topic.csdn.net/u/20100517/17/b2ab9d5e-73a2-4f54-a7ec-40a5eabd8621.html

作者: dawugui   发布时间: 2011-11-19

引用 3 楼 a_tuzi 的回复:

- -貌似 你的这个方式不可以啊。。 max是干嘛 最大值啊 我要的是所有的学生啊
而且我这个每个人都要显示啊。。。
不分组求出来的最大值 是所有成绩中的最大值吧


 列出“s-01”号课成绩比“s-02”号课成绩高的所有学生的学号

--------------------
表达不清楚时,最好用数据
贴一下以上数据显示结果

作者: roy_88   发布时间: 2011-11-19