当前位置:网站首页>SQL: 重新格式化部门表 (行转列问题:Group by + 聚合函数)
SQL: 重新格式化部门表 (行转列问题:Group by + 聚合函数)
2022-06-09 08:25:00 【解忧杂货铺Q】
部门表 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 查询来重新格式化表,使得新的表中有一个部门 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个月份的收入列)
使用 group by 需要使用聚合函数,这里的聚合函数可以用 max 、 min 、 sum 等
select
id,
sum(case when month='Jan' then revenue else null end) Jan_Revenue,
sum(case when month='Feb' then revenue else null end) Feb_Revenue,
sum(case when month='Mar' then revenue else null end) Mar_Revenue,
sum(case when month='Apr' then revenue else null end) Apr_Revenue,
sum(case when month='May' then revenue else null end) May_Revenue,
sum(case when month='Jun' then revenue else null end) Jun_Revenue,
sum(case when month='Jul' then revenue else null end) Jul_Revenue,
sum(case when month='Aug' then revenue else null end) Aug_Revenue,
sum(case when month='Sep' then revenue else null end) Sep_Revenue,
sum(case when month='Oct' then revenue else null end) Oct_Revenue,
sum(case when month='Nov' then revenue else null end) Nov_Revenue,
sum(case when month='Dec' then revenue else null end) Dec_Revenue
from Department
group by id
select
id,
max(case when month='Jan' then revenue else null end) Jan_Revenue,
max(case when month='Feb' then revenue else null end) Feb_Revenue,
max(case when month='Mar' then revenue else null end) Mar_Revenue,
max(case when month='Apr' then revenue else null end) Apr_Revenue,
max(case when month='May' then revenue else null end) May_Revenue,
max(case when month='Jun' then revenue else null end) Jun_Revenue,
max(case when month='Jul' then revenue else null end) Jul_Revenue,
max(case when month='Aug' then revenue else null end) Aug_Revenue,
max(case when month='Sep' then revenue else null end) Sep_Revenue,
max(case when month='Oct' then revenue else null end) Oct_Revenue,
max(case when month='Nov' then revenue else null end) Nov_Revenue,
max(case when month='Dec' then revenue else null end) Dec_Revenue
from Department
group by id
边栏推荐
- RMAN备份概念_关于备份集(Backup Set)
- belongsTo和hasOne的区别
- Open source EDA software yosys for integrated circuit design 1: tool installation
- RMAN备份概念_关于RMAN备份的多个拷贝
- JVM体系架构学习笔记
- Apple wins judge dismisses iPhone Security Fraud Class Action
- 2022-2028 investigation and trend analysis report on global anti UAV netting gun industry
- Market Research - current situation and future development trend of global and Chinese fish and vegetable symbiosis and hydroponic equipment market
- Market Research - current situation and future development trend of global and Chinese wall mounted extraction arm Market
- 2022-2028 global LED ribbon industry research and trend analysis report
猜你喜欢
随机推荐
Self made compiler learning 2: compilation process
Nacos 启动报错[db-load-error]load jdbc.properties error
系统运维系列 之greenplum vacuum清理删除数据命令
Elk cluster settings account password authentication
RMAN备份概念_关于RMAN备份的多个拷贝
Huayun data was selected as the representative manufacturer of cloud infrastructure in IDC development trends of industrial software and industrial Internet
About Eigendecomposition
Nacos二次开发
ELK集群设置账号密码认证
[reading papers] efficientnet: retailing model scaling for revolutionary neural networks
Blow up the idea artifact in use recently
. Net C # Foundation (6): namespace - a sharp tool for organizing code
集成电路设计开源EDA软件yosys详解1:工具安装
Quarkus实战学习一
[introduction to the paper] 2204 Vqgan-clip (open source):open domain image generation and editing with natural language guidance
About Matrix Decompositions
Leetcode basic programming: Search
刘勇智:一码通缺陷分析与架构设计方案丨声网开发者创业讲堂 Vol.02
RMAN备份数据库_指定备份输出选项
3D编程模式:依赖隔离模式








