[SQL문제풀기] Occupations

silver's avatar
Apr 15, 2025
[SQL문제풀기] Occupations

문제

MySQL

내가 작성한 오답

: case when은 각 행을 평가하여 조건에 맞으면 반환하고 아니면 null을 반환하기 때문에 저런 결과가 나온다
with occ as (select case when occupation = 'Actor' then name end Actor, case when occupation = 'Doctor' then name end Doctor, case when occupation = 'Professor' then name end Professor, case when occupation = 'Singer' then name end Singer from OCCUPATIONS) select doctor, professor, singer, actor from occ
notion image

내가 작성한 정답

: 같은 row에 출력하기 위해서 row_number()로 순번을 부여하여 group by 했다.
with num as (select name, occupation, row_number() over(partition by occupation order by name) nn from OCCUPATIONS) ,occ as (select max(case when occupation = 'Actor' then name end) Actor, max(case when occupation = 'Doctor' then name end) Doctor, max(case when occupation = 'Professor' then name end) Professor, max(case when occupation = 'Singer' then name end) Singer, nn from num group by nn) select doctor, professor, singer, actor from occ

Oracle

: union all을 하려는 각각의 select문 내에 order by 사용불가

내가 작성한 오답 - MySql의 정답과 동일

: 중간에 null이 출력이 됐다.
with num as (select name, occupation, row_number() over(partition by occupation order by name) nn from OCCUPATIONS) ,occ as (select max(case when occupation = 'Actor' then name end) Actor, max(case when occupation = 'Doctor' then name end) Doctor, max(case when occupation = 'Professor' then name end) Professor, max(case when occupation = 'Singer' then name end) Singer, nn from num group by nn) select doctor, professor, singer, actor from occ
notion image

내가 작성한 정답

: null을 뒤로 보내기 위해 order by nn을 추가했다.
with num as (select name, occupation, row_number() over(partition by occupation order by name) nn from OCCUPATIONS) ,occ as (select max(case when occupation = 'Actor' then name end) Actor, max(case when occupation = 'Doctor' then name end) Doctor, max(case when occupation = 'Professor' then name end) Professor, max(case when occupation = 'Singer' then name end) Singer, nn from num group by nn) select Doctor, Professor, Singer, Actor from occ order by nn;

PIVOT 사용한 정답

WITH num AS ( SELECT name, occupation, ROW_NUMBER() OVER(PARTITION BY occupation ORDER BY name) AS rn FROM OCCUPATIONS ) SELECT Doctor, Professor, Singer, Actor FROM num PIVOT ( MAX(name) FOR occupation IN ( 'Doctor' AS Doctor, 'Professor' AS Professor, 'Singer' AS Singer, 'Actor' AS Actor ) ) ORDER BY rn;

MS SQL Server

: mysql과 동일
with num as (select name, occupation, row_number() over(partition by occupation order by name) nn from OCCUPATIONS) ,occ as (select max(case when occupation = 'Actor' then name end) Actor, max(case when occupation = 'Doctor' then name end) Doctor, max(case when occupation = 'Professor' then name end) Professor, max(case when occupation = 'Singer' then name end) Singer, nn from num group by nn) select doctor, professor, singer, actor from occ;

DB2

내가 작성한 오답

: null이 null이 아닌 -으로 출력이 된다.
select occ from ((select name||'('||substr(occupation,1,1)||')' occ, 0 s, null o from OCCUPATIONS) union all (select 'There are a total of '||count(*)||' '||lower(occupation)||'s.' occ , 1 s, count(*) o from OCCUPATIONS group by occupation)) order by s asc, case when s=0 then occ else to_char(o) end;

내가 작성한 정답

with num as (select name, occupation, row_number() over(partition by occupation order by name) nn from OCCUPATIONS) ,occ as (select max(case when occupation = 'Actor' then name end) Actor, max(case when occupation = 'Doctor' then name end) Doctor, max(case when occupation = 'Professor' then name end) Professor, max(case when occupation = 'Singer' then name end) Singer, nn from num group by nn) select nvl(Doctor,'NULL'), nvl(Professor,'NULL'), nvl(Singer,'NULL'), nvl(Actor,'NULL') from occ;

정리!

💡
pivot - 행(Row) 에 있던 데이터를 열(Column) 로 바꾸는 것
 
Share article

silver