部门表 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个月份的收入列)

# Write your MySQL query statement below
select `id`,max(if(`month` = 'Jan', revenue, null)) as "Jan_Revenue",max(if(`month` = 'Feb', revenue, null)) as "Feb_Revenue",max(if(`month` = 'Mar', revenue, null)) as "Mar_Revenue",max(if(`month` = 'Apr', revenue, null)) as "Apr_Revenue",max(if(`month` = 'May', revenue, null)) as "May_Revenue",max(if(`month` = 'Jun', revenue, null)) as "Jun_Revenue",max(if(`month` = 'Jul', revenue, null)) as "Jul_Revenue",max(if(`month` = 'Aug', revenue, null)) as "Aug_Revenue",max(if(`month` = 'Sep', revenue, null)) as "Sep_Revenue",max(if(`month` = 'Oct', revenue, null)) as "Oct_Revenue",max(if(`month` = 'Nov', revenue, null)) as "Nov_Revenue",max(if(`month` = 'Dec', revenue, null)) as "Dec_Revenue"
fromDepartment
group by `id`;

leetcode 1179相关推荐

  1. LeetCode MySQL解题目录

    已完成的 LeetCode MySQL 数据库题目.点击查看我的 LeetCode 算法解题目录. 已解决 123/123 - 简单 54 中等 51 困难 18 前置入门学习 MySQL 基本查询. ...

  2. MySQL学习笔记(持续更新ING)

    SQL语法SQL 语法SQL语法 SQL对大小写不敏感:SELECT与select是相同的.SQL 对大小写不敏感:SELECT 与 select 是相同的.SQL对大小写不敏感:SELECT与sel ...

  3. LeetCode MySQL 1179. 重新格式化部门表

    文章目录 1. 题目 2. 解题 1. 题目 部门表 Department: +---------------+---------+ | Column Name | Type | +--------- ...

  4. [LeetCode]135.Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  5. LeetCode数据库题目1-123

    LeetCode数据库题目1-123 175. 组合两个表 难度简单 SQL架构 表1: Person +-------------+---------+ | 列名 | 类型 | +--------- ...

  6. LeetCode数据库SQL题目记录(难度:简单)

    难度:简单 目录 175. 组合两个表 176. 第二高的薪水 181. 超过经理收入的员工 182. 查找重复的电子邮箱 183. 从不订购的客户 196. 删除重复的电子邮箱 197. 上升的温度 ...

  7. LeetCode 从零单刷个人笔记整理(持续更新)

    更新至2020.2.23 github:https://github.com/ChopinXBP/LeetCode-Babel 本人博客用于个人对知识点的记录和巩固. 用几乎所有可行的方法进行了实现和 ...

  8. LeetCode刷SQL题

    https://leetcode-cn.com/problemset/database/ 题目都是leetcode 上了可以点击题目会有相应的链接 由于个人比较喜欢用开窗函数,所以都优先用了开窗 ,当 ...

  9. leetcode数据库题目1-123题(20-08-14)(1)

    难度简单 SQL架构 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | i ...

  10. 【LeetCode】(55)Jump Game(Medium)

    题目 Jump Game Total Accepted: 52275 Total Submissions: 194395My Submissions Question  Solution  Given ...

最新文章

  1. Y1ng‘s BabyCode
  2. 【运维技术】Zookeeper单机以及集群搭建教程
  3. 计算机基础知识教案1,计算机基础知识教案一
  4. [Leetcode][第112题][JAVA][路径总和][递归][队列]
  5. 那天,他无意间瞟了眼程序员的桌面……
  6. linux下sendmail邮件系统安装操作记录
  7. 宝宝的成长脚印9/29
  8. webpack3+node+react+babel实现热加载(hmr)
  9. 手把手教你进行R语言的安装及安装过程中相关问题解决方案
  10. 文件夹里没有index.html,gatsby-cli建立后没有index.html文件吗?
  11. like语句太慢 sqlserver_SQLSERVER查询速度慢的原因收集以及优化建议
  12. matlab如何泰勒公式用求近似值_Matlab 数值计算泰勒公式
  13. 利用树莓派完成POS58打印机的无驱动打印(初学者)
  14. PDF文档加密签名处理
  15. linux下制作dos启动u盘启动,linux dos启动盘怎样做
  16. oled屏幕(IIC接口+1306驱动)+raspberrypi pico 显示基于RT-Thread
  17. 偏见与苛求在科技媒体中依旧根深蒂固
  18. Pygame实战之外星人入侵NO.5——操作子弹
  19. 详解Linux终端下编写“贪吃蛇”游戏
  20. 二、Eureka之server端集群节点发现,数据同步

热门文章

  1. 一维搜索之黄金分割法
  2. 安防监控摄像机云储存如何开通
  3. 驱动开发遇到version magic不匹配
  4. HDU1728 BFS
  5. 引流工具GoReplay简介和在猪齿鱼效能平台中的应用
  6. Python 中的列表(一)
  7. 联想电脑如何关闭/开启windows自动更新
  8. AOP核心概念,连接点(JoinPoint)切入点(Pointcut)通知(Advice)通知类切面(Aspect)
  9. 带隙基准电压-Bandgap详细介绍
  10. C#ObjectArx Cad添加图层