일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- 성적프로그램
- 회고
- LeetCode
- git
- If
- hackerrank
- Jackson
- 항해99
- Python
- 책리뷰
- 서평
- 코드숨
- 스터디
- 뇌정리
- 2020년 일정
- 2020년 제4회 정보처리기사 필기 문제 분석
- algorithms
- Til
- 2020년 정보처리기사 4회
- 미니프로젝트
- 필기
- java
- sqldeveloper
- post
- 알고리즘
- 주간회고
- Real MySQL
- jsp
- 함수형 코딩
- 정보처리기사
- Today
- Total
조컴퓨터
leetcode 1179. Reformat Department Table 본문
1) CASE WHEN
1-1)
SELECT id
, CASE WHEN month = "Jan" THEN revenue ELSE NULL END AS Jan_Revenue
, CASE WHEN month = "Feb" THEN revenue ELSE NULL END AS Feb_Revenue
, CASE WHEN month = "Mar" THEN revenue ELSE NULL END AS Mar_Revenue
, CASE WHEN month = "Apr" THEN revenue ELSE NULL END AS Apr_Revenue
, CASE WHEN month = "May" THEN revenue ELSE NULL END AS May_Revenue
, CASE WHEN month = "Jun" THEN revenue ELSE NULL END AS Jun_Revenue
, CASE WHEN month = "Jul" THEN revenue ELSE NULL END AS Jul_Revenue
, CASE WHEN month = "Aug" THEN revenue ELSE NULL END AS Aug_Revenue
, CASE WHEN month = "Sep" THEN revenue ELSE NULL END AS Sep_Revenue
, CASE WHEN month = "Oct" THEN revenue ELSE NULL END AS Oct_Revenue
, CASE WHEN month = "Nov" THEN revenue ELSE NULL END AS Nov_Revenue
, CASE WHEN month = "Dec" THEN revenue ELSE NULL END AS Dec_Revenue
FROM Department
1-1) 결과
{"headers": ["id", "Jan_Revenue", "Feb_Revenue", "Mar_Revenue", "Apr_Revenue", "May_Revenue", "Jun_Revenue", "Jul_Revenue", "Aug_Revenue", "Sep_Revenue", "Oct_Revenue", "Nov_Revenue", "Dec_Revenue"],
"values": [[1, 8000, null, null, null, null, null, null, null, null, null, null, null], [2, 9000, null, null, null, null, null, null, null, null, null, null, null], [3, null, 10000, null, null, null, null, null, null, null, null, null, null], [1, null, 7000, null, null, null, null, null, null, null, null, null, null], [1, null, null, 6000, null, null, null, null, null, null, null, null, null]]}
1-2) GROUP BY
SELECT id
, CASE WHEN month = "Jan" THEN revenue ELSE NULL END AS Jan_Revenue
, CASE WHEN month = "Feb" THEN revenue ELSE NULL END AS Feb_Revenue
, CASE WHEN month = "Mar" THEN revenue ELSE NULL END AS Mar_Revenue
, CASE WHEN month = "Apr" THEN revenue ELSE NULL END AS Apr_Revenue
, CASE WHEN month = "May" THEN revenue ELSE NULL END AS May_Revenue
, CASE WHEN month = "Jun" THEN revenue ELSE NULL END AS Jun_Revenue
, CASE WHEN month = "Jul" THEN revenue ELSE NULL END AS Jul_Revenue
, CASE WHEN month = "Aug" THEN revenue ELSE NULL END AS Aug_Revenue
, CASE WHEN month = "Sep" THEN revenue ELSE NULL END AS Sep_Revenue
, CASE WHEN month = "Oct" THEN revenue ELSE NULL END AS Oct_Revenue
, CASE WHEN month = "Nov" THEN revenue ELSE NULL END AS Nov_Revenue
, CASE WHEN month = "Dec" THEN revenue ELSE NULL END AS Dec_Revenue
FROM Department
GROUP BY id
1-2) 결과
{"headers": ["id", "Jan_Revenue", "Feb_Revenue", "Mar_Revenue", "Apr_Revenue", "May_Revenue", "Jun_Revenue", "Jul_Revenue", "Aug_Revenue", "Sep_Revenue", "Oct_Revenue", "Nov_Revenue", "Dec_Revenue"],
"values": [[1, 8000, null, null, null, null, null, null, null, null, null, null, null], [2, 9000, null, null, null, null, null, null, null, null, null, null, null], [3, null, 10000, null, null, null, null, null, null, null, null, null, null]]}
2) SUM
2-1)
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
2-1) 결과
{"headers": ["id", "Jan_Revenue", "Feb_Revenue", "Mar_Revenue", "Apr_Revenue", "May_Revenue", "Jun_Revenue", "Jul_Revenue", "Aug_Revenue", "Sep_Revenue", "Oct_Revenue", "Nov_Revenue", "Dec_Revenue"],
"values": [[1, 8000, 7000, 6000, null, null, null, null, null, null, null, null, null], [2, 9000, null, null, null, null, null, null, null, null, null, null, null], [3, null, 10000, null, null, null, null, null, null, null, null, null, null]]}
SUCCESS!!
'LeetCode > SQL' 카테고리의 다른 글
leetcode 197. Rising Temperature (0) | 2021.01.18 |
---|---|
leetcode 181. Employees Earning More Than Their Managers (0) | 2021.01.18 |