LeetCode/릿코드-Reformat Department Table-MySQL

cosmos·2021년 12월 31일
0
post-thumbnail
post-custom-banner

문제


풀이

  • Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.
  • Return the result table in any order.
  • The query result format is in the following example.

쿼리

-- leetcode, reformat department table, mysql
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 
ORDER BY id 

결과


출처 && 깃허브

leetcode
Github

post-custom-banner

0개의 댓글