当前位置: 首页 > news >正文

SQL-leetcode—1179. 重新格式化部门表

1179. 重新格式化部门表

表 Department:

±--------------±--------+
| Column Name | Type |
±--------------±--------+
| id | int |
| revenue | int |
| month | varchar |
±--------------±--------+
在 SQL 中,(id, month) 是表的联合主键。
这个表格有关于每个部门每月收入的信息。
月份(month)可以取下列值 [“Jan”,“Feb”,“Mar”,“Apr”,“May”,“Jun”,“Jul”,“Aug”,“Sep”,“Oct”,“Nov”,“Dec”]。

重新格式化表格,使得 每个月 都有一个部门 id 列和一个收入列。

以 任意顺序 返回结果表。

结果格式如以下示例所示。

示例 1:

输入:
Department table:
±-----±--------±------+
| 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 列用于各个月份)。

题解

格式化表格,使得 每个月 都有一个部门 id 列和一个收入列

  • 经典的行转列,可以使用聚合函数+group by + case when来实现

方法一 SUM + group by

selectid,SUM(case when month='Jan' then revenue else null end) as Jan_Revenue,SUM(case when month='Feb' then revenue else null end) as Feb_Revenue,SUM(case when month='Mar' then revenue else null end) as Mar_Revenue,SUM(case when month='Apr' then revenue else null end) as Apr_Revenue,SUM(case when month='May' then revenue else null end) as May_Revenue,SUM(case when month='Jun' then revenue else null end) as Jun_Revenue,SUM(case when month='Jul' then revenue else null end) as Jul_Revenue,SUM(case when month='Aug' then revenue else null end) as Aug_Revenue,SUM(case when month='Sep' then revenue else null end) as Sep_Revenue,SUM(case when month='Oct' then revenue else null end) as Oct_Revenue,SUM(case when month='Nov' then revenue else null end) as Nov_Revenue,SUM(case when month='Dec' then revenue else null end) as Dec_Revenue
from Department
group by id

方法二 MAX + group by

selectid,MAX(case when month='Jan' then revenue else null end) as Jan_Revenue,MAX(case when month='Feb' then revenue else null end) as Feb_Revenue,MAX(case when month='Mar' then revenue else null end) as Mar_Revenue,MAX(case when month='Apr' then revenue else null end) as Apr_Revenue,MAX(case when month='May' then revenue else null end) as May_Revenue,MAX(case when month='Jun' then revenue else null end) as Jun_Revenue,MAX(case when month='Jul' then revenue else null end) as Jul_Revenue,MAX(case when month='Aug' then revenue else null end) as Aug_Revenue,MAX(case when month='Sep' then revenue else null end) as Sep_Revenue,MAX(case when month='Oct' then revenue else null end) as Oct_Revenue,MAX(case when month='Nov' then revenue else null end) as Nov_Revenue,MAX(case when month='Dec' then revenue else null end) as Dec_Revenue
from Department
group by id

方法三 MIN + group by

selectid,MIN(case when month='Jan' then revenue else null end) as Jan_Revenue,MIN(case when month='Feb' then revenue else null end) as Feb_Revenue,MIN(case when month='Mar' then revenue else null end) as Mar_Revenue,MIN(case when month='Apr' then revenue else null end) as Apr_Revenue,MIN(case when month='May' then revenue else null end) as May_Revenue,MIN(case when month='Jun' then revenue else null end) as Jun_Revenue,MIN(case when month='Jul' then revenue else null end) as Jul_Revenue,MIN(case when month='Aug' then revenue else null end) as Aug_Revenue,MIN(case when month='Sep' then revenue else null end) as Sep_Revenue,MIN(case when month='Oct' then revenue else null end) as Oct_Revenue,MIN(case when month='Nov' then revenue else null end) as Nov_Revenue,MIN(case when month='Dec' then revenue else null end) as Dec_Revenue
from Department
group by id

可能一开始看到SUM、MAX、MIN会不理解为啥?

在这里插入图片描述
在这里插入图片描述

可以看下这2个图例呢?

中间分组的过程其实是内部存储的,无法查询出来的一个虚拟的结果,一个框是一个集合的内容,这样的话就比较好理解为啥用聚合函数了。

如果不使用聚合函数会怎么样呢?
如果不使用的话,行数不会减少,会和输入数据一样的行数,就需要考虑一个合并的问题了。
大致效果是:
1, 100,null,null,null,…
2,null,100,null,null,…
1,null,100,null,null,…

显然id=1的数据没有合并,违背了行转列的预期效果。

分析案例

解题思路
由于筛选结果中每个ID是一个记录 因此GROUP BY ID.
每个月份是一列,因此筛选每个月份时使用CASE [when…then…] END只取当前月份.
需要使用SUM()聚合函数 因为如果没有聚合函数 筛选出来的是
GROUP BY、CASE…END之后的第一行.

比如 Department 表:
+------+---------+-------+
| id   | revenue | month |
+------+---------+-------+
| 1    | 8000    | Jan   |
| 2    | 9000    | Jan   |
| 3    | 10000   | Feb   |
| 1    | 7000    | Feb   |
| 1    | 6000    | Mar   |
+------+---------+-------+GROUP BY ID
+------+---------+-------+
| id   | revenue | month |
+------+---------+-------+
| 1    | 8000    | Jan   |
| 1    | 7000    | Feb   |
| 1    | 6000    | Mar   |
-------------------------
| 2    | 9000    | Jan   |
-------------------------
| 3    | 10000   | Feb   |
+------+---------+-------+如果没有聚合函数 只输出第一行 比如
SELECT ID, (CASE WHEN MONTH='JAN' THEN REVENUE END) AS JAN_REVENUE, 
(CASE WHEN MONTH='FEB' THEN REVENUE END) AS FEB_REVENUE  
FROM DEPARTMENT GROUP BY ID
会输出
+------+-------------+-------------+
| ID   | JAN_REVENUE | FEB_REVENUE |
+------+-------------+-------------+
| 1    | 8000        | NULL        |
| 2    | 7000        | NULL        |
| 3    | NULL        | 10000       |
+------+-------------+-------------+
其中 ID=1 的 FEB_REVENUE 结果不对,这是因为 ID=1 时, (CASE WHEN MONTH='FEB' THEN REVENUE END)= [NULL, 7000, NULL], 没有聚合函数会只取第一个,即NULL
http://www.lryc.cn/news/525351.html

相关文章:

  • JavaWeb 学习笔记 XML 和 Json 篇 | 020
  • 在Raspbian上,如何获取树莓派的CPU当前频率
  • 网络打印机的搜索与连接(一)
  • LangChain + llamaFactory + Qwen2-7b-VL 构建本地RAG问答系统
  • 【自然语言处理(NLP)】介绍、发展史
  • 1.CSS的三大特性
  • 【分布式日志篇】从工具选型到实战部署:全面解析日志采集与管理路径
  • 基于springcloud汽车信息分析与可视化系统
  • TOGAF之架构标准规范-信息系统架构 | 数据架构
  • Databend x 沉浸式翻译 | 基于 Databend Cloud 构建高效低成本的业务数据分析体系
  • cuda的并行运算介绍
  • 「全网最细 + 实战源码案例」设计模式——抽象工厂模式
  • 领域驱动设计(DDD)四 订单管理系统实践步骤
  • leetcode 面试经典 150 题:简化路径
  • 基于 STM32 的智能农业温室控制系统设计
  • 【Spring Boot】掌握 Spring 事务:隔离级别与传播机制解读与应用
  • 【Postgres_Python】使用python脚本将多个PG数据库合并为一个PG数据库
  • Tailwind CSS v4.0 发布
  • pandas基础:文件的读取和写入
  • 【MySQL — 数据库增删改查操作】深入解析MySQL的create insert 操作
  • 每日OJ_牛客_小红的子串_滑动窗口+前缀和_C++_Java
  • HTTP 配置与应用(局域网)
  • ultralytics 是什么?
  • AI竞争:从技术壁垒到用户数据之争
  • MySQL 主从复制(单组传统复制,GTID复制。双主复制)
  • python学opencv|读取图像(四十)掩模:三通道图像的局部覆盖
  • vue3 中如何监听 props 中的值的变化
  • Scrapy之一个item包含多级页面的处理方案
  • hive 自动检测、自动重启、记录检测日志、自动清理日志
  • HFSS同轴替换波端口