行转列经典题

行转列
--经典面试题一
create table test(
id number(10) primary key,
type number(10) ,
t_id number(10),
value varchar2(6)
);

insert into test values(100,1,1,'张三');
insert into test values(200,2,1,'男');
insert into test values(300,3,1,'50');

insert into test values(101,1,2,'刘二');
insert into test values(201,2,2,'男');
insert into test values(301,3,2,'30');

insert into test values(102,1,3,'刘三');
insert into test values(202,2,3,'女');
insert into test values(302,3,3,'10');
select * from test;
select max (decode(type,1,value))"姓名",
max (decode(type,2,value))"性别",
max (decode(type,3,value))"年龄"
from test group by t_id;
/*
利用一条sql语句将上表的数据显示为如下内容:
姓名 性别 年龄
----------------------
张三 男 50
刘二 男 30
刘三 女 10
*/
select (decode(type,1,value)) from test

select t1.value "姓名",t2.value "性别",t3.value "年龄" from
(select value,t_id from test where type = 1) t1 ,
(select value,t_id from test where type = 2) t2 ,
(select value,t_id from test where type = 3) t3 where t1.t_id = t2.t_id and t1.t_id = t3.t_id

--经典面试题二
create table tmp(rq varchar2(10),shengfu varchar2(5));

insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-10','胜');
insert into tmp values('2005-05-10','负');
insert into tmp values('2005-05-10','负');
----------------------------------
/*
日期 胜 负
2005-05-09 2 2
2005-05-10 1 2
*/
select * from tmp;
select rq "日期",count(decode(shengfu,'胜',1))"胜",
count(decode(shengfu,'负',1))"负"
from tmp group by rq order by rq asc


--经典面试题四
create table STUDENT_SCORE
(
name VARCHAR2(20),
subject VARCHAR2(20),
score NUMBER(4,1)
)
insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '语文', 78.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '数学', 88.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '英语', 98.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '语文', 89.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '数学', 76.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '英语', 90.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '语文', 99.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '数学', 66.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '英语', 91.0);

select * from student_score
/*3.请教一个面试中遇到的SQL语句的查询问题
表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
*/

select case when a>b then a else b end, case when a>b then a else b end from table01

/*
4.1得到类似下面的结果
姓名 语文 数学 英语

王五 89 56 89
*/
select name "名字",
sum(decode(subject, '语文', score))"语文",
sum(decode(subject, '数学', score))"数学",
sum(decode(subject, '英语', score))"英语"
from student_score
group by name
select t1.name, t1.score "语文", t2.score "数学", t3.score "英语"
from (select * from student_score where subject = '语文') t1,
(select * from student_score where subject = '数学') t2,
(select * from student_score where subject = '英语') t3
where t1.name = t2.name
and t1.name = t3.name

/*
4.2有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
------------------------------------------
*/

--4.2用case when去做
select a.name,(case when a.语文 >= 80 then '优秀'
when a.语文 >= 60 and a.语文 < 80 then '及格'
when a.语文 < 60 then '不及格'
end) "语文",
(case when a.数学 >= 80 then '优秀'
when a.数学 >= 60 and a.数学 < 80 then '及格'
when a.数学 < 60 then '不及格'
end) "数学",
(case when a.英语 >= 80 then '优秀'
when a.英语 >= 60 and a.英语 < 80 then '及格'
when a.英语 < 60 then '不及格'
end) "英语"
from
(select s.name,sum((case when s.subject='语文' then s.score else 0 end)) "语文",
sum((case when s.subject='数学' then s.score else 0 end)) "数学",
sum((case when s.subject='英语' then s.score else 0 end)) "英语"
from STUDENT_SCORE s group by s.name) a


/*
5.请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,
只是作为一个格式向大家请教。

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

table2
部门dep 部门名称dname
--------------------------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部

table3 (result)
部门dep 一月份 二月份 三月份
--------------------------------------
01 10 null null
02 10 8 null
03 null 5 8
04 null null 9


create table yj01(
month varchar2(10),
deptno number(10),
yj number(10)
);

insert into yj01(month,deptno,yj) values('一月份',01,10);
insert into yj01(month,deptno,yj) values('二月份',02,10);
insert into yj01(month,deptno,yj) values('二月份',03,5);
insert into yj01(month,deptno,yj) values('三月份',02,8);
insert into yj01(month,deptno,yj) values('三月份',04,9);
insert into yj01(month,deptno,yj) values('三月份',03,8);

create table yjdept(
deptno number(10),
dname varchar2(20)
);

insert into yjdept(deptno,dname) values(01,'国内业务一部');
insert into yjdept(deptno,dname) values(02,'国内业务二部');
insert into yjdept(deptno,dname) values(03,'国内业务三部');
insert into yjdept(deptno,dname) values(04,'国际业务部');

*/
select sum(case when t.month='一月份'then t.yj end) "一月份",
sum(case when t.month='二月份'then t.yj end) "二月份",
sum(case when t.month='三月份'then t.yj end) "三月份",
sum(case when t.month='四月份'then t.yj end) "四月份",
sum(case when t.month='五月份'then t.yj end) "五月份",
sum(case when t.month='六月份'then t.yj end) "六月份",
sum(case when t.month='七月份'then t.yj end) "七月份",
sum(case when t.month='八月份'then t.yj end) "八月份",
sum(case when t.month='九月份'then t.yj end) "九月份",
sum(case when t.month='十月份'then t.yj end) "十月份",
sum(case when t.month='十一月份'then t.yj end) "十一月份",
sum(case when t.month='十二月份'then t.yj end) "十二月份"
from yj01 t group by t.month;

 

转载于:https://www.cnblogs.com/CTR614308155/p/10732257.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值