【leetcode】1179. Reformat Department Table

答案

select id, max(case when month = 'Jan' then revenue end) as Jan_Revenue,
max(case when month = 'Feb' then revenue end) as Feb_Revenue,
max(case when month = 'Mar' then revenue end) as Mar_Revenue,
max(case when month = 'Apr' then revenue end) as Apr_Revenue,
max(case when month = 'May' then revenue end) as May_Revenue,
max(case when month = 'Jun' then revenue end) as Jun_Revenue,
max(case when month = 'Jul' then revenue end) as Jul_Revenue,
max(case when month = 'Aug' then revenue end) as Aug_Revenue,
max(case when month = 'Sep' then revenue end) as Sep_Revenue,
max(case when month = 'Oct' then revenue end) as Oct_Revenue,
max(case when month = 'Nov' then revenue end) as Nov_Revenue,
max(case when month = 'Dec' then revenue end) as Dec_Revenue

from Department
group by id

如果覺得太囉嗦可以直接看解釋。

case

參考:
https://blog.csdn.net/helloxiaozhe/article/details/78124138

有input_expression

CASE input_expression
WHEN when_expression THEN
    result_expression
WHEN when_expression THEN
    result_expression 
ELSE
    else_result_expression
END
  • 當遇到input_expression輸入時,滿足when_expression條件,則值爲result_expression,不符合when_expression條件值爲else_result_expression

舉例:

SELECT
    CASE parent_id
WHEN 0 THEN
    '00'
WHEN 1 THEN
    '11'
ELSE
    'OTHERS'
END AS parent_id_new ,
parent_id ,
type_id ,
type_name
FROM
    tdb_goods_types

沒有input_expression

CASE
WHEN Boolean_expression THEN
    result_expression
    [...n ] 
ELSE
    else_result_expression
END
  • 當滿足Boolean_expression條件,則值爲result_expression,不符合Boolean_expression條件值爲else_result_expression
    例子
ELECT
    CASE
WHEN parent_id < 3 THEN
    '<3'
WHEN parent_id >= 3
AND parent_id < 5 THEN
    '>=3 && <5'
ELSE
    '>=5'
END AS parent_id_new ,
parent_id ,
type_id ,
type_name
FROM
    tdb_goods_types

組函數

avg、sum、min、max、count
參考:https://www.cnblogs.com/geaozhang/p/6745147.html#sum-avg

group by

  • 根據字段進行分類,select字段部分使用組函數,不然where得到的記錄,如果針對group by字段是多個記錄,則會根據group by選擇第一個記錄
    參考: https://blog.csdn.net/u014180504/article/details/79150492
  • 如果需要對多條group by記錄均顯示,用with rollup子句
    參考: https://www.cnblogs.com/geaozhang/p/6745147.html#sum-avg

mysql總結

  • 先where做首輪刷選,再進行group by,再進行having刷選
  • where條件不能跟組函數,因爲where在group by後面執行,所以沒法進行
  • having可以不結合group by使用;having子句中的列,要麼出現在一個組函數中,要麼出現在group by子句中

講解

  • 當mouth是Feb時,Feb_Revenue字段設置爲當前記錄的revenue值
case when month = 'Feb' then revenue end as Feb_Revenue
  • 爲什麼要使用max,因爲where生成當前每個記錄針對id、Jan_Revenue … Dec_Revenue等的記錄,然而要根據group by id進行分組,則針對id多條記錄的會選擇第一條(只有一個月份有工資,其他沒有,爲0),所以需要在id多條記錄的進行聚合選擇,所以max
max(case when month = 'Feb' then revenue end) as Feb_Revenue

參考

https://www.cnblogs.com/strengthen/p/11484248.html
https://blog.csdn.net/helloxiaozhe/article/details/78124138
https://www.cnblogs.com/geaozhang/p/6745147.html
https://blog.csdn.net/u014180504/article/details/79150492

練習

做完這道題,學到了mysql中如何使用case

之前178. Rank Scores
學會了變量用法

可以試着做下180. Consecutive Numbers
我就是使用變量+case 做出來的,很爽啊,有沒有。。
如果還有些問題,可以看下我寫的sql

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章