你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle复杂查询练习题

[日期:2012-10-17] 来源:Linux社区  作者:johnjobs [字体:大 中 小]

5.得出所有人(不区分人员)每个月及上月和下月的总收入

  1. create table t5 (  tmonth int,   
  2. tname varchar2(10),   
  3. income number   
  4. )   
  5. insert into t5 values('08','a',1000);   
  6. insert into t5 values('09','a',2000);   
  7. insert into t5 values('10','a',3000);   
  8.  
  9. /*方案一*/ 
  10. select o.tmonth,sum(o.income) as cur,(select sum(t.income) from t5 t where t.tmonth=(o.tmonth+1) group by t.tmonth) as next,   
  11. (select sum(t.income) from t5 t where t.tmonth=(o.tmonth-1) group by t.tmonth) as last   
  12. from t5 o where o.tmonth=2 group by o.tmonth 
  13.  
  14. /*方案二*/ 
  15. select tmonth as 月份 ,tname as 姓名,sum(income) as 当月工资,   
  16. (select sum(income)     
  17. from t5     
  18. where tmonth = to_number(substr(to_char(sysdate,'yyyy-mm-dd'),7,1))-1) AS 上月工资 ,   
  19. (select sum(income)     
  20. from t5     
  21. where tmonth = to_number(substr(to_char(sysdate,'yyyy-mm-dd'),7,1))+1) AS 下月工资     
  22. from t5 where tmonth=substr(to_char(sysdate,'yyyy-mm-dd'),7,1)   
  23. group by tmonth,tname 

6.根据现有的学生表,课程表,选课关系表,查询一。没有修过李明老师的课的学生,查询二,既学过a课程,又学过b课程的学生姓名

  1. S表    [SNO,SNAME]--学生表 
  2. C表    [CNO,CNAME,CTEATHER] --课程表 
  3. SC表  [SNO,CNO,SCGRADE] --选课关系表 
  4.  
  5. 查询一:没有修过李明老师的课的学生的姓名 
  6. select sname from s where not exists 
  7. (select*from sc,c where sc.cno=c.cno and c.cteather='李明' and sc.sno=s.sno) 
  8.  
  9. 查询二:既学过a课程,又学过b课程的学生姓名 
  10. SELECT S.SNO,S.SNAME   
  11. FROM S,(   
  12.      SELECT SC.SNO   
  13.      FROM SC,C   
  14.      WHERE SC.CNO=C.CNO   
  15.          AND C.CNAME IN('a','b')   
  16.      GROUP BY SNO   
  17. )SC WHERE S.SNO=SC.SNO   
  18.  
  19. 查询三: 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩   
  20. SELECT S.SNO,S.SNAME,AVG(SC.SCGRADE) 
  21. FROM S,SC,(   
  22.      SELECT SNO   
  23.      FROM SC   
  24.      WHERE SCGRADE <60   
  25.      GROUP BY SNO   
  26.      HAVING COUNT(DISTINCT CNO)>=2   
  27. )A WHERE S.SNO=A.SNO AND SC.SNO=A.SNO   
  28. GROUP BY S.SNO,S.SNAME 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款