设为首页 加入收藏

TOP

oracle面试sql(一)
2014-11-23 19:10:57 来源: 作者: 【 】 浏览:17
Tags:oracle 面试 sql

1、关于group by表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负


如果要生成下列结果, 该如何写sql语句


胜 负
2005-05-09 2 2
2005-05-10 1 2


答案:


1) select rq,sum(case when shengfu=’胜’ then 1 else 0 end) as胜,sum(case whenshengfu=’负’ then 1 else 0 end) asfrom tab3 group by rq
2) select N.rq,N. 胜,M. 负 from
(select rq,count(*) 胜 from tab3 where shengfu=’胜’group by rq)N inner join
(select rq,count(*) 负from tab3 where shengfu=’负’group by rq)M on N.rq=M.rq
3) select a.rq,a. 胜 as胜,b.负 asfrom
(select rq,count(shengfu) 胜from tab3 where shengfu=’胜’ group by rq) a,
(select rq,count(shengfu) 负from tab3 where shengfu=’负’ group by rq) b
where a.rq=b.rq;


1.关于group by表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负


如果要生成下列结果, 该如何写sql语句


胜 负
2005-05-09 2 2
2005-05-10 1 2
——————————————————————————————–
1) select rq,sum(case when shengfu=’胜’ then 1 else 0 end) as胜,sum(case whenshengfu=’负’ then 1 else 0 end) asfrom tab3 group by rq
2) select N.rq,N. 胜,M. 负 from
(select rq,count(*) 胜 from tab3 where shengfu=’胜’group by rq)N inner join
(select rq,count(*) 负from tab3 where shengfu=’负’group by rq)M on N.rq=M.rq
3) select a.rq,a. 胜 as胜,b.负 asfrom
(select rq,count(shengfu) 胜from tab3 where shengfu=’胜’ group by rq) a,
(select rq,count(shengfu) 负from tab3 where shengfu=’负’ group by rq) b
where a.rq=b.rq;
2.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
——————————————————————————————–
select (case when a>b then a else b end),(case when b>c then b else c end) fromtab4
3.一个日期判断的sql语句
请取出tab5表中日期(SendTime字段)为当天的所有记录 (SendTime字段为datetime型,包含日期与时间)
———————————————————————————————
select * from tab5 t where to_char(t.SendTime,’yyyy-mm-dd’)=to_char(sysdate,’yyyy-mm-dd’)
4.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
——————————————————————————————-
select
(case when语文>=80 then ’优秀’ when语文>60 then ’及格’ else ’不及格’ end) as 语文,
(case when 数学>=80 then ’优秀’ when数学>60 then ’及格’ else ’不及格’ end) as数学,
(case when英语>=80 then ’优秀’ when英语>60 then ’及格’ else ’不及格’ end) as 英语
from tab5


5.请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据


table1


月份mon 部门dep 业绩yj
——————————-
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8


table2


部门dep 部门名称depname
——————————–
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部


table3 (result)


部门dep 一月份 二月份 三月份
—————————————————
01 10 null null
02 10 8 null
03 5 null 8
04 null 9 null
——————————————————————————————-
1)
select t.depname,
(select yj from tab6 where mon=’一月份’ and dep=t.dep) 一月份,
(select yj from tab6 where mon=’二月份’ and dep=t.dep) 二月份,
(select yj from tab6 where mon=’三月份’ and dep=t.dep) 三月份
from tab7 t
———————————————————
2)求总销售额
select
sum(case when t1.mon=’一月份’ then t1.yj else 0 end) 一月份,
sum(case when t1.mon=’二月份’ then t1.yj else 0 end) 二月份,
sum(case when t1.mon=’三月份’ then t1.yj else 0 end) 三月份
from tab7 t,tab6 t1 where t.dep=t1.dep
6.一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
——————————————————————————-
select id,count(*) from tab8 group by id having count(*)>1
select * from (select tab8,count(id) as num from tab8 group by id) t where t.num>1


7.用一条SQL语句 查询出每门课都大于80分的学生姓名
name

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++开发工程师面试题库 50~100道 下一篇2012.10月上旬百度腾讯笔试面试

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: