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

歡迎訪問 生活随笔!

生活随笔

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

数据库

【LeetCode-SQL每日一练】—— 1179. 重新格式化部门表

發布時間:2023/12/10 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode-SQL每日一练】—— 1179. 重新格式化部门表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?🎈寫在前面

🙋?♂?大家好呀,我是超夢。大家可以叫我小夢~

又到了練習SQL的時候啦!一起來學習吧!

🙋?♂? 小伙伴們如果在學習過程中有不明白的地方,歡迎評論區留言提問,小夢定知無不言,言無不盡。


目錄

🌌SQL題目

🌌解題思路

🌌方法實現

🌌代碼測試

🌌知識點小結

🌌往期推薦



🌌SQL題目

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


🌌解題思路

department 表中存儲這所有人所有月的收入,這里的需求是將 department 的 month 列拆成具體的月份。

?? 1.? 首先將 department 根據 id 分組。
?? 2.? 使用 case month when 'Jan' then revenue end 計算出一月份的收入,(也可以使用 if(month = 'Jan', revenue, null))。12個月都按照這個方式寫完。


🌌方法實現

根據上述解題思路,我們有兩種方式解決

方法一:

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

方法二:

select id,sum(if(month = 'Jan', revenue, null)) as 'Jan_Revenue',sum(if(month = 'Feb', revenue, null)) as 'Feb_Revenue',sum(if(month = 'Mar', revenue, null)) as 'Mar_Revenue',sum(if(month = 'Apr', revenue, null)) as 'Apr_Revenue',sum(if(month = 'May', revenue, null)) as 'May_Revenue',sum(if(month = 'Jun', revenue, null)) as 'Jun_Revenue',sum(if(month = 'Jul', revenue, null)) as 'Jul_Revenue',sum(if(month = 'Aug', revenue, null)) as 'Aug_Revenue',sum(if(month = 'Sep', revenue, null)) as 'Sep_Revenue',sum(if(month = 'Oct', revenue, null)) as 'Oct_Revenue',sum(if(month = 'Nov', revenue, null)) as 'Nov_Revenue',sum(if(month = 'Dec', revenue, null)) as 'Dec_Revenue' from department group by id;


🌌代碼測試

?與預期結果一致,測試成功!執行所需時間 160s


??與預期結果一致,測試成功!執行所需時間 196s



🌌知識點小結

🌀case when:

??? 簡單函數(枚舉這個字段所有可能的值)
??? CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END

??? 搜索函數(搜索函數可以寫判斷,并且搜索函數只會返回第一個符合條件的值,其他case被忽略)

CASE WHEN [expr] THEN [result1]…ELSE [default] END

🌀if()函數:

??????? IF函數根據條件的結果為true或false,返回第一個值,或第二個值。

IF(condition, value_if_true, value_if_false)


🌌往期推薦

🚀【LeetCode-SQL每日一練】—— 627. 變更性別

🚀【LeetCode-SQL每日一練】—— 620. 有趣的電影

🚀【LeetCode-SQL每日一練】—— 196. 刪除重復的電子郵箱



總結

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

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