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

晕了, 简单的SQL题
SNO     CNO       DEGREE
----   -----   ---------------------------------------
103     3-245   86.0
105     3-245   75.0
109     3-245   68.0
103     3-105   92.0
105     3-105   88.0
109     3-105   76.0
101     3-105   64.0
107     3-105   91.0
108     3-105   78.0
101     6-166   85.0
107     6-166   79.0
108     6-166   80.0
105     6-166   82.0

(13   行受影响)
SNO           --成绩ID
    CNO         --课程编号
    DEGREE       --分数

--33、查询成绩比该课程平均成绩低的同学的成绩表。

--34查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

--35查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,


------解决方案--------------------
create table tab(sno int,cno varchar(20),degree float)
insert into tab select 103, '3-245 ',86
union
select 105, '3-245 ',75


select * from tab t1 where degree <(select avg(degree) from tab t2 where t2.cno=t1.cno)
-------------------
105 3-245 75.0

------解决方案--------------------
create table tab1(SNO int, CNO varchar(20), DEGREE dec(15,1))
insert tab1 select 103, '3-245 ', 86.0
union all select 105, '3-245 ', 75.0
union all select 109, '3-245 ', 68.0
union all select 103, '3-105 ', 92.0
union all select 105, '3-105 ', 88.0
union all select 109, '3-105 ', 76.0
union all select 101, '3-105 ', 64.0
union all select 107, '3-105 ', 91.0
union all select 108, '3-105 ', 78.0
union all select 101, '6-166 ', 85.0
union all select 107, '6-166 ', 79.0
union all select 108, '6-166 ', 80.0
union all select 105, '6-166 ', 82.0

--33、查询成绩比该课程平均成绩低的同学的成绩表。
select * from
tab1 a
left join
(select CNO,sum(DEGREE)/count(1)de from tab1 group by CNO)b
on b.cno=a.cno
where b.de> a.degree