日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2024/7/5 数据库 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode MySQL 1179. 重新格式化部门表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

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

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/reformat-department-table
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

2. 解題

  • 按部門id分組,組合 case 月份 when + 求和
# Write your MySQL query statement below select id,sum(case when month = 'Jan' then revenue else null end) as Jan_Revenue,sum(case when month = 'Feb' then revenue else null end) as Feb_Revenue,sum(case when month = 'Mar' then revenue else null end) as Mar_Revenue,sum(case when month = 'Apr' then revenue else null end) as Apr_Revenue,sum(case when month = 'May' then revenue else null end) as May_Revenue,sum(case when month = 'Jun' then revenue else null end) as Jun_Revenue,sum(case when month = 'Jul' then revenue else null end) as Jul_Revenue,sum(case when month = 'Aug' then revenue else null end) as Aug_Revenue,sum(case when month = 'Sep' then revenue else null end) as Sep_Revenue,sum(case when month = 'Oct' then revenue else null end) as Oct_Revenue,sum(case when month = 'Nov' then revenue else null end) as Nov_Revenue,sum(case when month = 'Dec' then revenue else null end) as Dec_Revenue from Department group by id

我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!

總結

以上是生活随笔為你收集整理的LeetCode MySQL 1179. 重新格式化部门表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。