今天可以不用加班,出去溜达了一圈还略微迷路了。。哈哈哈
问题9 查询和"01"号的同学学习的课程完全相同的其他同学的信息(正确解法在下面)
这个题网上答案就是针对这个表的数据做查询,不能推广。
下面给出我搜到的一个答案,这个查询是通过选择与学号01的学生课程数量相同的学生 的信息。
select student.*
from student
where sid in (select sid from score group by sid having count(cid) = (select count(cid) from score where sid ='01')
and sid<> '01');
这个题也不能用lead开窗函数做,因为也不能推广。
select *
from student
where sid in
(select b.sid from
(select sid, cid, lead(cid,1) over(partition by sid order by cid) cid2,
lead(cid,2) over(partition by sid order by cid) cid3 from score) a,
(select sid, cid, lead(cid,1) over(partition by sid order by cid) cid2,
lead(cid,2) over(partition by sid order by cid) cid3 from score) b
where a.cid=b.cid and a.cid2=b.cid2 and a.cid3=b.cid3 and a.sid='01')
--a表筛选出来学号sid=01的学生,b表找符合要求的学生
and sid not in('01')--排除掉学号01学生自身;
这个查询的思路是 考虑到学号01的学生以及课程表course只有cid为01 02 03的三门课,用lead开窗函数把所有学生的 每门课成绩作为新的字段,用学生01的每门课的成绩与其余行的对应成绩对比。
其中,由lead创建的新的表的语句和运行结果如下:
select sid, cid,
lead(cid,1) over(partition