题目如下:

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的更多相关文章

  1. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【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 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. 对于富文本编辑器中使用lazyload图片懒加载

    使用lazyload.js图片懒加载的作用是给用户一个好的浏览体验,同时对服务器减轻了压力,当用户浏览到该图片的时候再对图片进行加载,项目中使用lazyload的时候需要将图片加入data-orgin ...

  2. 使用 Blender* 重新拓扑 VR 和游戏素材

    本文介绍如何将网格重新拓扑成一个整洁的低密度模型,然后 UV 解包该网格,以便将纹理贴添加至新模型.本文还将探讨如何使用免费工具,比如 Blender* 及其 Bsurface 插件,重新拓扑雕塑的 ...

  3. ef core 动态拼接 条件

    var sql = new List<string>(); var sqlparams = new List<string>(); ; foreach (var p in ph ...

  4. 几个关于json序列化 的注解

    一.@JsonProperty 1.此注解用于属性上,作用是把该属性的名称序列化为另外一个名称.例如: @JsonProperty("name") private String N ...

  5. MySQL-4- 索引及执行计划

    1. 索引作用 提供了类似于书中目录的作用,目的是为了优化查询 2. 索引的种类(算法) B树索引 Hash索引 R树 Full text GIS 3. B树 基于不同的查找算法分类介绍   B-tr ...

  6. noip2013day2-华容道

    题目描述 小 \(B\) 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用 编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多 少时间. 小 \(B ...

  7. 洛谷 P3388 【模板】割点(割顶)(Tarjan)

    题目链接 https://www.luogu.org/problemnew/show/P3388 模板题 解题思路 什么是割点? 怎样求割点? dfn :即时间戳,一张图的dfs序(dfs遍历时出现的 ...

  8. python-day12(正式学习)

    目录 可变长参数 可变长形参之* 可变长实参之* 可变长形参之** 可变长实参之** 可变长参数应用 命名关键字形参 函数对象 四大功能 引用 当作参数传给一个函数 可以当作函数的返回值 可以当作容器 ...

  9. Cookie、Session和LocalStorage

    前记 前面我已经写了一篇关于Cookie的文章,但是那时候我其实理解的并不是很深刻,会有些搞不懂的地方,今天我就再写一次,博客也是我的学习笔记 Cookie Cookie是服务器发送到用户浏览器并保存 ...

  10. 12、MA图的计算过程

    为了简化问题,假设有3张芯片,每组数有9个探针: Data: 2,4,6,7,9,10,4,7,8,3 9,5,3,2,5,7,9,10,3,12 6,4,3,2,7,8,1,2,6,9 一.给3组数 ...