数据分析SQL面试题目9套-第3、4题(笔记)

66 篇文章 0 订阅

题目来源:https://www.jianshu.com/p/0f165dcf9525

第3题

1.1 题目(行转列)

在这里插入图片描述
建表语句:

-- 创建 course 表
create table course (
id varchar(20),
teacher_id varchar(20),
week_day varchar(20),
has_course varchar(20)
);
insert into course value
(1,1,2,"Yes"),
(2,1,3,"Yes"),
(3,2,1,"Yes"),
(4,3,2,"Yes"),
(5,1,2,"Yes");

1.2 解题

select teacher_id,
       (case when week_day='1' then 'YES' else '' end) as 'mon',
       (case when week_day='2' then 'YES' else '' end) as 'tue',
       (case when week_day='3' then 'YES' else '' end) as 'thi',
       (case when week_day='4' then 'YES' else '' end) as 'thu',
       (case when week_day='5' then 'YES' else '' end) as 'fri'
from course;

第4题

2.1 题目(列转行)

在这里插入图片描述
建表语句:

create table a1 (
name varchar(20),
english int,
maths int,
music int);

insert into a1 values
("Jim",90,88,99);

2.2 解题

select name, 'english' as subject, english as score from a1
union all
select name, 'maths' as subject, maths as score from a1
union all
select name, 'music' as subject, music as score from a1
order by name;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值