🎈写在前面
🙋♂️大家好呀,我是超梦。大家可以叫我小梦~
又到了练习SQL的时候啦!一起来学习吧!
🙋♂️ 小伙伴们如果在学习过程中有不明白的地方,欢迎评论区留言提问,小梦定知无不言,言无不尽。
目录
🌌SQL题目
🌌解题思路
🌌方法实现
🌌代码测试
🌌知识点小结
🌌往期推荐
🌌SQL题目
🌀部门表
Department
:+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | revenue | int | | month | varchar | +---------------+---------+ (id, month) 是表的联合主键。 这个表格有关于每个部门每月收入的信息。 月份(month)可以取下列值 ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]。
题目:
编写一个 SQL 查询来重新格式化表,使得新的表中有一个部门 id 列和一些对应 每个月 的收入(revenue)列。
查询结果格式如下面的示例所示:
Department 表: +------+---------+-------+ | id | revenue | month | +------+---------+-------+ | 1 | 8000 | Jan | | 2 | 9000 | Jan | | 3 | 10000 | Feb | | 1 | 7000 | Feb | | 1 | 6000 | Mar | +------+---------+-------+查询得到的结果表: +------+-------------+-------------+-------------+-----+-------------+ | id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue | +------+-------------+-------------+-------------+-----+-------------+ | 1 | 8000 | 7000 | 6000 | ... | null | | 2 | 9000 | null | null | ... | null | | 3 | null | 10000 | null | ... | null | +------+-------------+-------------+-------------+-----+-------------+注意,结果表有 13 列 (1个部门 id 列 + 12个月份的收入列)。
🌌解题思路
department 表中存储这所有人所有月的收入,这里的需求是将 department 的 month 列拆成具体的月份。
1. 首先将 department 根据 id 分组。
2. 使用 case month when 'Jan' then revenue end 计算出一月份的收入,(也可以使用 if(month = 'Jan', revenue, null))。12个月都按照这个方式写完。
🌌方法实现
根据上述解题思路,我们有两种方式解决
方法一:
select id,sum(case month when 'Jan' then revenue end) as 'Jan_Revenue',sum(case month when 'Feb' then revenue end) as 'Feb_Revenue',sum(case month when 'Mar' then revenue end) as 'Mar_Revenue',sum(case month when 'Apr' then revenue end) as 'Apr_Revenue',sum(case month when 'May' then revenue end) as 'May_Revenue',sum(case month when 'Jun' then revenue end) as 'Jun_Revenue',sum(case month when 'Jul' then revenue end) as 'Jul_Revenue',sum(case month when 'Aug' then revenue end) as 'Aug_Revenue',sum(case month when 'Sep' then revenue end) as 'Sep_Revenue',sum(case month when 'Oct' then revenue end) as 'Oct_Revenue',sum(case month when 'Nov' then revenue end) as 'Nov_Revenue',sum(case month when 'Dec' then revenue end) as 'Dec_Revenue' from department group by id;
方法二:
select id,sum(if(month = 'Jan', revenue, null)) as 'Jan_Revenue',sum(if(month = 'Feb', revenue, null)) as 'Feb_Revenue',sum(if(month = 'Mar', revenue, null)) as 'Mar_Revenue',sum(if(month = 'Apr', revenue, null)) as 'Apr_Revenue',sum(if(month = 'May', revenue, null)) as 'May_Revenue',sum(if(month = 'Jun', revenue, null)) as 'Jun_Revenue',sum(if(month = 'Jul', revenue, null)) as 'Jul_Revenue',sum(if(month = 'Aug', revenue, null)) as 'Aug_Revenue',sum(if(month = 'Sep', revenue, null)) as 'Sep_Revenue',sum(if(month = 'Oct', revenue, null)) as 'Oct_Revenue',sum(if(month = 'Nov', revenue, null)) as 'Nov_Revenue',sum(if(month = 'Dec', revenue, null)) as 'Dec_Revenue' from department group by id;
🌌代码测试
与预期结果一致,测试成功!执行所需时间 160s
与预期结果一致,测试成功!执行所需时间 196s
🌌知识点小结
🌀case when:
简单函数(枚举这个字段所有可能的值)
CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END搜索函数(搜索函数可以写判断,并且搜索函数只会返回第一个符合条件的值,其他case被忽略)
CASE WHEN [expr] THEN [result1]…ELSE [default] END
🌀if()函数:
IF函数根据条件的结果为true或false,返回第一个值,或第二个值。
IF(condition, value_if_true, value_if_false)
🌌往期推荐
🚀【LeetCode-SQL每日一练】—— 627. 变更性别
🚀【LeetCode-SQL每日一练】—— 620. 有趣的电影
🚀【LeetCode-SQL每日一练】—— 196. 删除重复的电子邮箱