Oracle中国移动经典面试题(附代码跟两种答案)

/*
中国移动sql面试题:

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;
*/

/*
利用一条sql语句将上表的数据显示为如下内容:
姓名     性别     年龄
----------------------
张三     男      50
刘二     男      30
刘三     女      10
*/

--使用分组方式
select max(decode(type, 1, value)) "姓名",
       max(decode(type, 2, value)) "性别",
       max(decode(type, 3, value)) "年龄"
  from test
 group by t_id

--使用表连接方式
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

转载于:https://www.cnblogs.com/shenzhenhuicnblogs/p/10742330.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值