news 2026/4/15 23:43:38

力扣1179-重新格式化部门表

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
力扣1179-重新格式化部门表

Department

+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | revenue | int | | month | varchar | +---------------+---------+ 在 SQL 中,(id, month) 是表的联合主键。 这个表格有关于每个部门每月收入的信息。 月份(month)可以取下列值 ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]。

重新格式化表格,使得每个月都有一个部门 id 列和一个收入列。

任意顺序返回结果表。

结果格式如以下示例所示。

示例 1:

输入:Department table: +------+---------+-------+ | 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 列用于各个月份)。

思路:

1,本题主要为列转行

2,可以使用case when 或者poivt

3,部门id分组,sum(每个月的金额)

代码:

select id, sum(case when month = 'Jan' then revenue else null end) Jan_Revenue, sum(case when month = 'Feb' then revenue else null end) Feb_Revenue, sum(case when month = 'Mar' then revenue else null end) Mar_Revenue, sum(case when month = 'Apr' then revenue else null end) Apr_Revenue, sum(case when month = 'May' then revenue else null end) May_Revenue, sum(case when month = 'Jun' then revenue else null end) Jun_Revenue, sum(case when month = 'Jul' then revenue else null end) Jul_Revenue, sum(case when month = 'Aug' then revenue else null end) Aug_Revenue, sum(case when month = 'Sep' then revenue else null end) Sep_Revenue, sum(case when month = 'Oct' then revenue else null end) Oct_Revenue, sum(case when month = 'Nov' then revenue else null end) Nov_Revenue, sum(case when month = 'Dec' then revenue else null end) Dec_Revenue from Department group by id

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 14:29:27

Sonic模型训练数据来源公开吗?是否存在偏见风险

Sonic模型训练数据透明度与偏见风险探析 在虚拟人技术加速落地的今天,一个简单的问题正在引发越来越多关注:我们看到的“完美”数字人,背后是否藏着看不见的偏见?当一张照片加一段音频就能生成栩栩如生的说话视频时,人…

作者头像 李华
网站建设 2026/4/16 12:25:17

Windows 11 删除字体

不能删的字体1. 系统界面核心字体 (删除后系统立刻崩溃/乱码)Segoe UI 系列(这是 Win10/11 的灵魂字体,整个系统界面都靠它)Segoe MDL2 Assets / Segoe Fluent Icons(由于 Win11 的很多图标其实是字体,删了这个&#x…

作者头像 李华
网站建设 2026/4/15 16:36:40

粉丝二创受限吗?非商用可宽容对待

粉丝二创受限吗?非商用可宽容对待 在虚拟偶像直播带货频频出圈、AI主播24小时不间断播报新闻的今天,一个更现实的问题悄然浮现:普通用户能不能用自己的方式,为喜欢的角色“配音”?比如,让某个经典动漫人物念…

作者头像 李华
网站建设 2026/4/16 18:12:20

Sonic能否集成到Zoom/Teams?远程会议新玩法

Sonic能否集成到Zoom/Teams?远程会议新玩法 在远程办公成为常态的今天,几乎每个人都经历过那种“镜头前疲惫不堪”的感觉:连续几小时盯着屏幕开会,强打精神保持微笑,生怕走神被点名。更别提跨时区协作时凌晨三点上线、…

作者头像 李华
网站建设 2026/4/16 14:49:15

算法——前缀和

前缀和与差分的核心思想是预处理,可以在暴力枚举的过程中,快速给出查询的结果,从而优化时间复杂度。是经典的用空间替换时间的做法。 一、一维前缀和 快速求出数组中,某一段区间的和 1.先预处理出一个前缀和数组 ①f [ i ] 表…

作者头像 李华
网站建设 2026/4/16 16:47:13

亲测好用8个AI论文平台,本科生轻松搞定毕业论文!

亲测好用8个AI论文平台,本科生轻松搞定毕业论文! AI 工具如何成为论文写作的得力助手 随着人工智能技术的不断进步,越来越多的本科生开始借助 AI 工具来辅助自己的毕业论文写作。这些工具不仅能够帮助学生高效完成论文的初稿、大纲搭建&#…

作者头像 李华