【leetcode】1179. Reformat Department Table
题目如下:
Table:
Department+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| revenue | int |
| month | varchar |
+---------------+---------+
(id, month) is the primary key of this table.
The table has information about the revenue of each department per month.
The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.
The query result format is in the following example:
Department table:
+------+---------+-------+
| id | revenue | month |
+------+---------+-------+
| 1 | 8000 | Jan |
| 2 | 9000 | Jan |
| 3 | 10000 | Feb |
| 1 | 7000 | Feb |
| 1 | 6000 | Mar |
+------+---------+-------+ Result table:
+------+-------------+-------------+-------------+-----+-------------+
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1 | 8000 | 7000 | 6000 | ... | null |
| 2 | 9000 | null | null | ... | null |
| 3 | null | 10000 | null | ... | null |
+------+-------------+-------------+-------------+-----+-------------+ Note that the result table has 13 columns (1 for the department id + 12 for the months).
解题思路:难得有免费的SQL题目。我的SQL性能比较低,就是做12次左连接,求出每个月的revenue。
代码如下:
# Write your MySQL query statement below
select d0.id, Jan_Revenue,Feb_Revenue,Mar_Revenue,Apr_Revenue,May_Revenue,Jun_Revenue,Jul_Revenue,
Aug_Revenue,Sep_Revenue,Oct_Revenue,Nov_Revenue,Dec_Revenue from
(select distinct id from Department order by id ) d0
left join
(select id,revenue as Jan_Revenue from Department where month = 'Jan') d1 on d0.id = d1.id
left join
(select id,revenue as Feb_Revenue from Department where month = 'Feb') d2 on d0.id = d2.id
left join
(select id,revenue as Mar_Revenue from Department where month = 'Mar') d3 on d0.id = d3.id
left join
(select id,revenue as Apr_Revenue from Department where month = 'Apr') d4 on d0.id = d4.id
left join
(select id,revenue as May_Revenue from Department where month = 'May') d5 on d0.id = d5.id
left join
(select id,revenue as Jun_Revenue from Department where month = 'Jun') d6 on d0.id = d6.id
left join
(select id,revenue as Jul_Revenue from Department where month = 'Jul') d7 on d0.id = d7.id
left join
(select id,revenue as Aug_Revenue from Department where month = 'Aug') d8 on d0.id = d8.id
left join
(select id,revenue as Sep_Revenue from Department where month = 'Sep') d9 on d0.id = d9.id
left join
(select id,revenue as Oct_Revenue from Department where month = 'Oct') d10 on d0.id = d10.id
left join
(select id,revenue as Nov_Revenue from Department where month = 'Nov') d11 on d0.id = d11.id
left join
(select id,revenue as Dec_Revenue from Department where month = 'Dec') d12 on d0.id = d12.id
order by id;
【leetcode】1179. Reformat Department Table的更多相关文章
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- java游戏服务器--简单工厂模式
先来学习下简单工厂模式! 我们知道在游戏里有很多的场景,例如:帮派场景,副本场景,野外场景... 现在我们有这样的需求: 1.我们需要进入帮派场景时---开始执行帮派任务. 2.我们需要进入副本场景时 ...
- 基于高斯分布的异常检测(Anomaly Detection)算法
记得在做电商运营初期,每每为我们频道的促销活动锁取得的“超高”销售额感动,但后来随着工作的深入,我越来越觉得这里面水很深.商家运营.品类运营不断的通过刷单来获取其所需,或是商品搜索排名,或是某种kpi ...
- windows OS安全配置【持续更新20190618】
https://www.52stu.org/?p=76 来源:5号暗区 5号黯区 五号黯区 5号暗区 windows系统的一些加固方法等 关闭445端口: REG ADD HKLM\SYSTEM\Cu ...
- Akka系列(八):Akka persistence设计理念之CQRS
前言........ 这一篇文章主要是讲解Akka persistence的核心设计理念,也是CQRS(Command Query Responsibility Segregation)架构设计的典型 ...
- ubuntu 设置sudo 免密码
一. 修改sudoers的权限 二. 修改sudoers 文件 <1>. 在文件最后一行添加yourusername ALL=(ALL) NOPASSWD : ALL 三. 修改回sudo ...
- 问题:C++类的静态成员变量如何初始化
C++类的静态成员变量属于该类,在该类所有的对象间共享. 要弄清如何初始化,首先要明白声明.定义.初始化三个概念的不同. 声明:指定变量的名字和类型,可以多次声明. 定义:为该成员变量分配存储空间,有 ...
- C++线性表通过结构体实现操作和结构体字符串快速排序和shell排序结合
#include<iostream> #include<string> #define ml 10 using namespace std; typedef struct{// ...
- gcc 数据对齐之:总结篇.
通过上面的分析,总结结构体对齐规则如下: 1.数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragm ...
- npm学习(四)之如何安装全局包、更新全局安装的包、卸载全局安装的包
如何安装全局包 有两种方式用来安装 npm 包:本地安装和全局安装.选用哪种方式来安装,取决于你如何使用这个包. 如果你想将其作为一个命令行工具,那么你应该将其安装到全局.这种安装方式后可以让你在任何 ...
- 优化 Karatsuba 乘法(老物)
虽然写好了我自己用的a*启发函数但还是有些不尽人意,如果通过数学分析确定不出问题可以工作了的话应该就会发出来了 // Karatsuba 递归式距离推导 // h(x) = f(x) * g(x):/ ...