【MySql】- CASE WHEN

MySql 触发器里的流程控制语句: CASE WHEN

CASE WHEN 语法有两种:

  1. 简单函数。
    CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
  2. 搜索函数。
    CASE WHEN [expr] THEN [result1]…ELSE [default] END

简单函数

SELECT
    id,
    `name`,
    (
        CASE sex
        WHEN 1 THEN
            "男"
        WHEN 2 THEN
            "女"
        ELSE
            "其他"
        END
    ) AS sex
FROM
    user_info

搜索函数

SELECT
    id,
    `name`,
    (
        CASE 
        WHEN sex=1 THEN
            "男"
        WHEN sex=2 THEN
            "女"
        ELSE
            "其他"
        END
    ) AS sex
FROM
    user_info

CASE WHEN 行转列

leetcode #### 1179. 重新格式化部门表
部门表 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语句

CREATE TABLE `department` (
  `id` bigint(20) NOT NULL,
  `revenue` bigint(20) DEFAULT NULL,
  `month` varchar(64) NOT NULL,
  PRIMARY KEY (`id`,`month`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of department
-- ----------------------------
INSERT INTO `department` VALUES ('1', '7000', 'Feb');
INSERT INTO `department` VALUES ('1', '8000', 'Jan');
INSERT INTO `department` VALUES ('1', '6000', 'Mar');
INSERT INTO `department` VALUES ('2', '9000', 'Jan');
INSERT INTO `department` VALUES ('3', '10000', 'Feb');

编写一个 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个月份的收入列)。

solution:

select id,
SUM(CASE WHEN `month` = "Jan" THEN revenue END) as Jan_Revenue,
SUM(CASE WHEN `month` = "Feb" THEN revenue END) as Feb_Revenue,
SUM(CASE WHEN `month` = "Mar" THEN revenue END) as Mar_Revenue,
SUM(CASE WHEN `month` = "Apr" THEN revenue END) as Apr_Revenue,
SUM(CASE WHEN `month` = "May" THEN revenue END) as May_Revenue,
SUM(CASE WHEN `month` = "Jun" THEN revenue END) as Jun_Revenue,
SUM(CASE WHEN `month` = "Jul"  THEN revenue END) as Jul_Revenue,
SUM(CASE WHEN `month` = "Aug" THEN revenue END) as Aug_Revenue,
SUM(CASE WHEN `month` = "Sep" THEN revenue END) as Sep_Revenue,
SUM(CASE WHEN `month` = "Oct" THEN revenue END) as Oct_Revenue,
SUM(CASE WHEN `month` = "Nov" THEN revenue END) as Nov_Revenue,
SUM(CASE WHEN `month` = "Dec" THEN revenue END) as Dec_Revenue
from department 
group by id
ORDER BY id

你可能感兴趣的:(【MySql】- CASE WHEN)