Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.12.29 - 오늘의 SQL #61

Merged
merged 4 commits into from
Dec 29, 2021
Merged

21.12.29 - 오늘의 SQL #61

merged 4 commits into from
Dec 29, 2021

Conversation

mildsalmon
Copy link
Owner

No description provided.

오라클 11 이후부터는 PIVOT함수가 있는 것을 알았다.
다만 그렇게 풀고 싶지 않았다.
그래서 DECODE를 사용해서 풀었는데, 전혀 감을 못잡겠더라.

그래서 PIVOT 함수로 풀었다.

우선, PARTITION BY 구문에서 직업별로 그룹을 만든다. 그리고 이름 순서로 중복없는 번호를 매긴다.
그 값을 Doctor, Professor, Singer, Actor 순서로 PIVOT하고 컬럼을 호출한다.
MAX(name)은 name값이 있으면 name을 없으면 NULL을 입력한다.
PARTITION BY 구문까지는 똑같다.
다만, rank로 group을 만들고 해당 group에서 Doctor, Professor, Singer, Actor를 추출했다.
여기서도 MAX를 사용하여 해당하는 값이 없으면 NULL이 반환된다.

마지막에 rank로 정렬하면 완성.
DECODE와 CASE는 서로 변경할 수 있다.
CASE문은 조건문과 반환, 끝이 어디인지 확실하게 알 수 있어서 더 가독성이 높은 것 같다.
@mildsalmon mildsalmon merged commit a0e18bc into master Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant