-
[MySQL] CASE WHEN ~ THEN ~ END로 pivot table 변환SQL 2021. 8. 22. 18:08
https://www.hackerrank.com/challenges/occupations/problem
https://techblog-history-younghunjo1.tistory.com/159
http://tcpschool.com/mysql/mysql_operator_flowControl
-- 각 직업별 개수를 세기 위한 변수 설정 set @D = 0, @P = 0, @S = 0, @A = 0; -- case when을 이용하여 pivot table 만들기 -- 집계 함수를 사용하는 이유는 group by로 묶기 위해서, null 처리를 위해서 select min(Doctor), min(Professor), min(Singer), min(Actor) from (select case when Occupation = 'Doctor' then Name end as Doctor, case when Occupation = 'Professor' then Name end as Professor, case when Occupation = 'Singer' then Name end as Singer, case when Occupation = 'Actor' then Name end as Actor, case when Occupation = 'Doctor' then (@D := @D + 1) when Occupation = 'Professor' then (@P := @P + 1) when Occupation = 'Singer' then (@S := @S + 1) when Occupation = 'Actor' then (@A := @A + 1) end as RowNumber from OCCUPATIONS order by Name) Sub group by RowNumber;
'SQL' 카테고리의 다른 글
[MySQL] IF 함수를 이용한 BST SELECT (0) 2021.08.23 [MySQL] CONCAT, LOWER, UPPER 함수 (0) 2021.08.23 [MySQL] SUBSTR 함수를 이용한 문자열 추출 (0) 2021.08.20 [MySQL] UNION, UNION ALL 함수 (0) 2021.08.19 [MySQL] MOD 함수를 이용한 짝수/홀수 행 SELECT (0) 2021.08.19