【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 ...
随机推荐
- golang基础学习-MongoDB使用
1.系统环境 Golang:go version go1.10.3 darwin/amd64 OS:MacOS MongoDB: version: 3.4.4 2.Golang使用MongoDB 使用 ...
- 【ABAP系列】SAP ABAP7.40新语法简介第一篇
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP7.40新语法简 ...
- ssh端口转发功能
一.SSH 端口转发能够提供两大功能: 1.加密SSH Client 端至SSH Server 端之间的通讯数据 2.突破防火墙的限制完成一些之前无法建立的TCP 连接 (隧道功能) 二:SSH端口 ...
- 【转帖】联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s
联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s https://www.cnbeta.com/articles/tech/855223.htm 国产主控 紫光做国产颗粒 国产器件对 ...
- 网络架构,七层协议,三次握手四次挥手,socket套接字简单编程
一.单机架构 应用领域: 植物大战僵尸 office 二.CS架构 应用领域: QQ 大型网络游戏 计算机发展初期用户去取数据,直接就去主机拿,从这里开始就分出了客户端和服务端. 客户端:用户安装的软 ...
- js中index()的四种经典用法(转https://blog.csdn.net/superit401/article/details/51726826)
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- Android 开源项目及库汇总(2)
Android 开源项目及库汇总(2) ListenToCode 2.7 2018.10.10 15:43 字数 8527 阅读 1001评论 0喜欢 29 地图 百度地图– Android百度地图 ...
- npm学习(四)之如何安装全局包、更新全局安装的包、卸载全局安装的包
如何安装全局包 有两种方式用来安装 npm 包:本地安装和全局安装.选用哪种方式来安装,取决于你如何使用这个包. 如果你想将其作为一个命令行工具,那么你应该将其安装到全局.这种安装方式后可以让你在任何 ...
- 定义一个外部类Father,有成员变量name并赋一个初值。
1.内部类的使用:(1)定义一个外部类Father,有成员变量name并赋一个初值.(2)定义一个内部类Child,并定义一个getValue()方法,在方法中调用外部类Father的name变量.( ...
- fullpage实现(-)
在线demo还没弄好,地址先给出来