当前位置:网站首页>Database daily question --- day 24: format department table
Database daily question --- day 24: format department table
2022-07-18 16:33:00 【InfoQ】
One 、 Problem description
Department+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| revenue | int |
| month | varchar |
+---------------+---------+
(id, month) Is the union primary key of the table .
This form has information about the monthly income of each department .
month (month) You can take the following values ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
SQL (revenue)Two 、 Subject requirements
Examples
Department surface :
+------+---------+-------+
| id | revenue | month |
+------+---------+-------+
| 1 | 8000 | Jan |
| 2 | 9000 | Jan |
| 3 | 10000 | Feb |
| 1 | 7000 | Feb |
| 1 | 6000 | Mar |
+------+---------+-------+
The result table obtained by query :
+------+-------------+-------------+-------------+-----+-------------+
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1 | 8000 | 7000 | 6000 | ... | null |
| 2 | 9000 | null | null | ... | null |
| 3 | null | 10000 | null | ... | null |
+------+-------------+-------------+-------------+-----+-------------+
Be careful , The result table has 13 Column (1 Departments id Column + 12 The revenue column for the last month ).
Investigate
1. Aggregate functions 、case sentence
2. It is recommended to use time 10~25min
3、 ... and 、 Problem analysis
12nullidcase Variable when Conditions then result 1 else result 2
idsumFour 、 coded
select id,
sum(case month when 'Jan' then revenue else null end) as Jan_Revenue,
sum(case month when 'Feb' then revenue else null end) as Feb_Revenue,
sum(case month when 'Mar' then revenue else null end) as Mar_Revenue,
sum(case month when 'Apr' then revenue else null end) as Apr_Revenue,
sum(case month when 'May' then revenue else null end) as May_Revenue,
sum(case month when 'Jun' then revenue else null end) as Jun_Revenue,
sum(case month when 'Jul' then revenue else null end) as Jul_Revenue,
sum(case month when 'Aug' then revenue else null end) as Aug_Revenue,
sum(case month when 'Sep' then revenue else null end) as Sep_Revenue,
sum(case month when 'Oct' then revenue else null end) as Oct_Revenue,
sum(case month when 'Nov' then revenue else null end) as Nov_Revenue,
sum(case month when 'Dec' then revenue else null end) as Dec_Revenue
from Department
group by id
5、 ... and 、 test result



边栏推荐
- MySQL小记
- 【今天的小go同学要去丢垃圾(1)】
- Selenium八大元素定位及相对定位器
- ModStart模块预发布功能上线啦
- 各国程序员薪资水平,咱有点惨...
- mysql进阶(四)聊聊mysql中的事务锁机制
- 学习笔记——直流电机调速器
- [vsctf2022]web topic recurrence
- Playing with "private e-commerce", is the chain 2+1 model worth enterprises' in-depth understanding?
- Sword finger offer 09 Implementing queues with two stacks
猜你喜欢
idea Gradle7.0+ :Could not find method compile()

OneFlow源码一览:GDB编译调试
![[bioinformatics] exosome miRNA growth training camp (14 days)](/img/ff/06bde421040aa80c7bfd7534335f40.png)
[bioinformatics] exosome miRNA growth training camp (14 days)

【分享】二层交换和三层交换转发

Atcoder beginer contest 259 partial solution

各国程序员薪资水平,咱有点惨...

Sword finger offer 09 Implementing queues with two stacks

JMeter如何使用MD5加密并且对body进行指纹签名

Mysql启动选项和配置文件

数据库每日一题---第24天:格式化部门表
随机推荐
PG operation and maintenance -- error log and slow log
Analysis of container health inspection
HCIP PPP/HDLC、GRE/MGRE实验
Elk cluster deployment (IV) deployment logstash
MySQL notes
The second week of summer vacation
UWA report uses tips. Did you get it? (the fifth bullet)
[tensorflow2.9] Titanic survival prediction - structured data modeling process
Tdsql PG version is upgraded again, and we are deeply involved in the construction of open source ecosystem
网络丢包,网络延迟?这款神器帮你搞定所有!
【刷题记录】12. 整数转罗马数字
工业级知识图谱de构建与应用(三):商品知识融合
[bioinformation] protein interaction project ideas Growth Camp (14 days)
Halcon 距离计算
Elk cluster deployment (V) deployment filebeat
Unity 基础 之 Camera摄像机属性介绍
5 data packets and LSA information of OSPF
高等数学---第八章隐函数偏导数与全微分
Unity 基础 之 目录结构解析
[QNX hypervisor 2.2 user manual]8.2 guest exit