首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

多表查查查查解决方法

2012-08-03 
多表查查查查学生表S学号 Sno姓名 Sname课程信息表C课程号 Cno课程名 Cname学生成绩表SC学号 Sno课程号 Cn

多表查查查查
学生表S
学号 Sno  
姓名 Sname

课程信息表C
课程号 Cno
课程名 Cname

学生成绩表SC
学号 Sno
课程号 Cno
成绩 Score

查询每门课程的最高成绩、最低成绩的学生姓名及分数

[解决办法]
sql2005

SQL code
create table S(Sno int,    Sname nvarchar(10))create table C(Cno int, Cname nvarchar(10))create table SC(Sno int,  Cno int, Score int)   insert into S  select 1,'张三'  union allselect 2,'李四' union allselect 3,'王五'insert into C select 10,'语文' union allselect 20,'数学' union allselect 30,'英语' insert into SCselect 1,10,90 union allselect 1,20,89 union allselect 1,30,88 union allselect 2,10,80 union allselect 2,20,79 union allselect 2,30,78 union allselect 3,10,60 union allselect 3,20,66 union allselect 3,30,100 ;with cte as( select *,rowNum=dense_rank() over(partition by Cno order by Score desc) from SC)select t1.Sno,t2.Sname,t1.Cno,t3.Cname,t1.Score from cte t1 left join S t2 on t1.Sno=t2.Sno left join C t3 on t1.Cno=t3.Cno where t1.rowNum=1Sno         Sname      Cno         Cname      Score----------- ---------- ----------- ---------- -----------1           张三         10          语文         901           张三         20          数学         893           王五         30          英语         100(3 行受影响) 

热点排行
Bad Request.