테이블 소개
:해당 테이블을 아래와 같이 배열하기
(직업별 의사 , 교수, 가수, 배우 순으로 배열하되, 해당되는 셀 값이 없으면 NULL 처리, 이름은 알파벳 순서대로 정렬)
SELECT
name,
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num
FROM employees;
결과값
SELECT
NAME,
OCCUPATION,
ROW_NUMBER(PARTION BY OCCUPATION ORDER BY NAME) AS "ROW_NUMBER"
FROM OCCUPATIONS
SELECT CASE WHEN OCCUPATION = 'DOCTOR' THEN NAME ELSE NULL END,
CASE WHEN OCCUPATION = 'PROFESSOR' THEN NAME ELSE NULL END,
CASE WHEN OCCUPATION = 'SINGER' THEN NAME ELSE NULL END,
CASE WHEN OCCUPATION = 'ACTOR' THEN NAME ELSE NULL END
FROM
ORDER BY ROW_NUM
ORDER BY ROW_NUM
SELECT
MAX(CASE WHEN Occupation = 'Doctor' THEN Name ELSE NULL END) AS Doctor,
MAX(CASE WHEN Occupation = 'Professor' THEN Name ELSE NULL END) AS Professor,
MAX(CASE WHEN Occupation = 'Singer' THEN Name ELSE NULL END) AS Singer,
MAX(CASE WHEN Occupation = 'Actor' THEN Name ELSE NULL END) AS Actor -- 그룹핑 하기 위한 MAX 사용
FROM (
SELECT Name, Occupation,
ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) AS RowNum
FROM OCCUPATIONS
) AS OrderedNames
GROUP BY RowNum
ORDER BY RowNum;