-
[MySQL] CASE WHEN ~ THEN ~ END로 pivot table 변환SQL 2021. 8. 22. 18:08
https://www.hackerrank.com/challenges/occupations/problem
Occupations | HackerRank
Pivot the Occupation column so the Name of each person in OCCUPATIONS is displayed underneath their respective Occupation.
www.hackerrank.com
https://techblog-history-younghunjo1.tistory.com/159
[SQL] CASE WHEN으로 Pivot Table 만들기(HackerRank - Occupations 문제)
🔊 본 포스팅에서 사용되는 테이블의 자료와 출처는 HackerRank 임을 밝힙니다. 더 다양한 SQL 문제를 풀어보시려면HackerRank 사이트를 방문해 보세요! 이번 포스팅에서는 MySQL의 CASE WHEN 구문으로 Pivo
techblog-history-younghunjo1.tistory.com
http://tcpschool.com/mysql/mysql_operator_flowControl
코딩교육 티씨피스쿨
4차산업혁명, 코딩교육, 소프트웨어교육, 코딩기초, SW코딩, 기초코딩부터 자바 파이썬 등
tcpschool.com
-- 각 직업별 개수를 세기 위한 변수 설정 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