设为首页 收藏本站
查看: 749|回复: 0

[经验分享] oracle常见的经典查询语句(二)

[复制链接]

尚未签到

发表于 2016-8-4 09:37:25 | 显示全部楼层 |阅读模式
01.   tmp表中有如下记录(建表 SQLemp.sql
 

RQ         SHENGFU

---------- ----------

2005-05-09 WIN

2005-05-09 WIN

2005-05-09 LOSE

2005-05-09 LOSE

2005-05-10 WIN

2005-05-10 LOSE

2005-05-10 LOSE
 
要求格式为:
 

RQ                WIN       LOSE

---------- ---------- ----------

2005-05-10          1          2

2005-05-09          2          2
 
 
答案:select rq,  count(case when shengfu='WIN' then 'WIN' else null end) WIN, count(case when shengfu='LOSE' then 'LOSE' else null end) LOSE from tmp group by rq ;
 
 
02.   查询当前月有多少天
 
答案:SQL> select trunc(add_months(sysdate,1),'month') - trunc(sysdate,'month') from dual;
 
 
03.   pages表有四个字段,id, url,title,body。如图:
 
ID        URL                    TITLE          BODY

--------- ----------- ------------------------- ------

1        http://www.baidu.com    新闻          党报评事业单位发绩效工资 砸铁饭碗再砸金饭碗

2        http://www.sina.com     baidu新闻     假唱假演奏最高罚款3000元 10月1日起施行

3        http://www.yahoo.com    搜索结果      www.baidu.com/search/url_submit.html - 网页快

4        http://www.baidu.com    新闻          垃圾焚烧产生致癌物 专家告诫中国勿重蹈日本覆辙

 

要求格式为:
 

ID                   CUNT

--------------------------------------------------------------------------------

3                    www.baidu.com/search/url_submit.html - 网页快照 - 类似结果

2                    baidu新闻

1                    http://www.baidu.com

4                    http://www.baidu.com
 
答案:select id,body cunt from pages where body  like '%baidu%' union all select id,title  from pages where title  like '%baidu%' union all select id,url from pages where url like '%baidu%';
 
ID                   URL

--------------------------------------------------------------------------------

1                    http://www.baidu.com

4                    http://www.baidu.com

2                    baidu新闻

3                    www.baidu.com/search/url_submit.html - 网页快照 - 类似结果

 

 

答案:select id,url from pages where url like '%baidu%' union all select id,title  from pages where title  like '%baidu%' union all select id,body cunt from pages where body  like '%baidu%';

 
 
04. 现有 STUDENT(学生), COURSE(课程), SC(成绩)表,完成以下需求(建表语句在 emp.sql
中,综合考察)
a)   查询选修课程为 web的学员学号和姓名
 
答案:SQL> Select s.name,s.sid from student s, (Select sid from sc Where cid=(select cid from course where name='web' )) s1 where s.sid=s1.sid;
 
b)  查询课程编号为 2的学员姓名和单位
 
答案:select * from student s,(select sid from  sc where cid=2) s1 where s.sid=s1.sid;
 
 
b)   查询不选修4号课程的学员姓名和单位
 
答案:SQL> select distinct s.* from student s,(Select sid from sc where cid!=4) s1 where s.sid=s1.sid;
 
 
d)  查询选修全部课程的学员姓名和单位
 
答案:SQL> select * from student where sid=(select sid from sc group by sid having  count(*)=(select count(distinct cid) from sc));
 
e)  查询选修课程超过 3门的学员姓名和单位
 
答案:SQL> select * from student s,(select sid from sc group by sid having  count(*)>3) s1 where s.sid=s1.sid;
 
f)  找出没有选修过 Teacher LI讲授课程的所有学生姓名
 
答案:select distinct s1.* from student s1 where s1.sid not in ( select  s.sid from sc s,(select distinct cid from course where  TEACHER='Teacher LI') c  where s.cid=c.cid) ;

 
 
g)  列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩 
 
答案:SQL> select s.*,sco from student s,(select distinct sid,avg(SCORE) sco from sc where score<60 group by sid) s1 where s.sid=s1.sid;
 
 
h)  列出既学过1号课程,又学过2号课程的所有学生姓名
 
答案:SQL> select * from student s, (select sid from sc where cid=1) s1 ,(select sid from sc where cid=2) s2 where s.sid=s1.sid and s.sid=s2.sid;
 
i)  列出1号课成绩比2号课成绩高的所有学生的学号,姓名和 1号课和 2号课的成

 
答案:select * from student s, (select sid,SCORE from sc where cid=1) s1 ,(select sid,SCORE from sc where cid=2) s2 where s.sid=s1.sid and s.sid=s2.sid and s1.score>s2.score;

 
 
05. 现有test表,表中数据如图所示:
  
a)  连续的编号要求如下格式
  

     BEGIN        END

---------- ----------

         1          6

         8          9

        11         14

        18         19
 
 
答案:Select min(id) begin,max(id) end from test group by id-rownum order by id-rownum
 
b)  不连续的编号要求如下格式
 
        BEGIN        END

---------- ----------

         8          9

        11         14

        18         19
 
 
答案:SQL> Select min(id) begin,max(id) end from test group by id-rownum having id-rownum!=0 order by id-rownum;
 
 
 
06.(统计各部门,各职位的人数)
 

DEPTNO      CLERK   SALESMAN  PRESIDENT    MANAGER    ANALYST

------ ---------- ---------- ---------- ---------- ----------

    30          1          4          0          1          0

    20          2          0          0          1          2

    10          1          0          1          1          0
 
答案:select deptno,  count(case when job='CLERK' then 'CLERK' else null end)  CLERK, count(case when job = 'SALESMAN' then 'SALESMAN' else null end)  SALESMAN, count(case when job='PRESIDENT' then 'PRESIDENT' else null end)  PRESIDENT, count(case when job='MANAGER' then 'MANAGER' else null end)  MANAGER, count(case when job='ANALYST' then 'ANALYST' else null end)  ANALYST from emp group by deptno;

 
 
07. 根据EMP表数据产生如下格式的报表(统计各职位,各部门的人数)(06题的变体)
 
 
 
Job                   10          20       30
------------- ---------- ----------- ---------
                   0          0          1

CLERK              1          2          1

SALESMAN           0          0          4

PRESIDENT          1          0          0

MANAGER            1          1          1

ANALYST            0          2          0
 
 
答案:select job ,  count(case when deptno='10' then '10' else null end) as "10",  count(case when deptno='20' then '20' else null end)  as "20",  count(case when deptno='30' then '30' else null end) as "30"  from emp group by job;

 
08. 按照如下格式显示 7369号员工的信息
 
Empno  key     value
------------ ------------ ---------------------------
7369    comm
7369               deptno   20          
7369               ename   smith
7369               hiredate                1980-12-17
7369               job                          cleak
7369               mgr                        7902
7369               sal                           800
 
 
答案:select empno, 'ENAME' as KEY, ename VALUE from emp where empno = 7369
  union 
select empno, 'JOB', job from emp where empno = 7369
  union 
select empno, 'HIREDATE', to_char(hiredate,'yyyy-mm-dd') a from emp where empno = 7369
  union 
select empno, 'MGR', to_char(mgr) from emp where empno = 7369
  union
select empno, 'SAL', to_char(sal) from emp where empno = 7369
  union 
select empno, 'COMM', to_char(comm) from emp where empno = 7369
  union
select empno, 'DEPTNO', to_char(deptno) from emp where empno = 7369;
 
 
 
附件中为数据库文件在上一篇中oracle经典查询案例中忘记上传数据库文件了

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-252598-1-1.html 上篇帖子: Oracle 拼接列数据的方法 下篇帖子: 分享:构建高可用性的Oracle架构
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表