加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift]LeetCode1179.

发布时间:2020-12-14 04:26:07 所属栏目:百科 来源:网络整理
导读:★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ?微信公众号:为敢(WeiGanTechnologies) ?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/) ?GitHub地址:https://github.com/strengthen/LeetCode ?原文

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公众号:为敢(WeiGanTechnologies)
?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/11484248.html
?如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
?原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Table:?Department

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| revenue       | int     |
| month         | varchar |
+---------------+---------+
(id,month) is the primary key of this table.
The table has information about the revenue of each department per month.
The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].

?

Write an SQL query to reformat the table such that there is a department id column?and a revenue column?for each month.

The query result format is in the following example:

Department table:
+------+---------+-------+
| id   | revenue | month |
+------+---------+-------+
| 1    | 8000    | Jan   |
| 2    | 9000    | Jan   |
| 3    | 10000   | Feb   |
| 1    | 7000    | Feb   |
| 1    | 6000    | Mar   |
+------+---------+-------+

Result table:
+------+-------------+-------------+-------------+-----+-------------+
| id   | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1    | 8000        | 7000        | 6000        | ... | null        |
| 2    | 9000        | null        | null        | ... | null        |
| 3    | null        | 10000       | null        | ... | null        |
+------+-------------+-------------+-------------+-----+-------------+

Note that the result table has 13 columns (1 for the department id + 12 for the months).

Runtime:?308 ms
 1 # Write your MySQL query statement below
 2 select id,max(case when month = Jan then revenue end) as Jan_Revenue, 3 max(case when month = Feb then revenue end) as Feb_Revenue, 4 max(case when month = Mar then revenue end) as Mar_Revenue, 5 max(case when month = Apr then revenue end) as Apr_Revenue, 6 max(case when month = May then revenue end) as May_Revenue, 7 max(case when month = Jun then revenue end) as Jun_Revenue, 8 max(case when month = Jul then revenue end) as Jul_Revenue, 9 max(case when month = Aug then revenue end) as Aug_Revenue,10 max(case when month = Sep then revenue end) as Sep_Revenue,11 max(case when month = Oct then revenue end) as Oct_Revenue,12 max(case when month = Nov then revenue end) as Nov_Revenue,13 max(case when month = Dec then revenue end) as Dec_Revenue
14 
15 from Department
16 group by id

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读