当前位置: 首页 > news >正文

力扣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

http://www.jsqmd.com/news/182954/

相关文章:

  • Spring AI 集成国内大模型实战:千问/豆包(含多模态)+ Spring Boot 4.0.1 全攻略
  • Sonic数字人可用于广告投放?案例分析ROI提升效果
  • 任务管理|基于java + vue任务管理系统(源码+数据库+文档)
  • 基于Sonic的数字人生成方案,助力短视频创作降本增效
  • 当AI开始懂你的学术焦虑:PaperXie毕业论文功能,不是代写,是“思维协作者
  • Sonic社区治理规则:维护健康生态人人有责
  • 打卡信奥刷题(2630)用C++实现信奥题 P2638 安全系统
  • 全网最全9个AI论文写作软件,MBA毕业论文必备!
  • 面试必杀:对比 LangChain 与 AutoGPT/BabyAGI 的本质差异——为什么工业界更倾向于‘可控图(Graph)’?
  • Sonic能否生成儿童/老人面孔?年龄适应性实测报告
  • iertutil.dll文件损坏丢失找不到 打不开程序 免费下载方法
  • DBA手记|报账租赁系统Oracle迁移卡壳?金仓数据库72小时实现“零感知”割接
  • Sonic数字人眨眼机制是预设还是音频驱动?揭秘细节
  • Sonic数字人背景替换技巧:结合绿幕抠像提升真实感
  • 临终关怀陪伴?Sonic提供安宁疗护话语
  • 全网口碑好的中石化加油卡回收平台推荐 - 京顺回收
  • ifmon.dll文件损坏丢失找不到 打不开程序 免费下载方法
  • Sonic数字人适配直播场景?超低延迟生成不是梦
  • C#能否调用Sonic DLL?跨语言集成的技术路径分析
  • Git commit规范提交Sonic项目代码,团队协作更高效
  • 深入解析:华为手机USB连接WIN11--ew_usbccgpfilter.sys驱动无法加载
  • 出租车管理|基于java+ vue出租车管理系统(源码+数据库+文档)
  • Typora官网推荐写作工具,撰写Sonic技术文档更流畅
  • 力扣hot100:最小栈的实现
  • 无需3D建模!使用Sonic数字人模型+静态图+音频快速生成说话视频
  • Three.js与Sonic结合?构建3D数字人交互应用新思路
  • 脑机接口控制Sonic数字人?远期设想
  • Sonic数字人眼神跟随功能?注视点模拟实现方式
  • Spring-boot读书笔记一Map-Filter-Reduce
  • Sonic数字人能否模仿明星?法律风险极高请勿尝试