【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

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