ABOUT ME

기록하고 기억하다.

Today
Yesterday
Total
  • hackerrank mysql Occupations 문제풀이
    데이터 분석/DB & SQL 2019. 12. 15. 16:11

    HackerRank Mysql : Occupations

     

    MySQL :: MySQL 8.0 Reference Manual :: 12.4 Control Flow Functions

    MySQL 8.0 Reference Manual  /  Functions and Operators  /  Control Flow Functions 12.4 Control Flow Functions Table 12.6 Flow Control Operators Name Description CASE Case operator IF() If/else construct IFNULL() Null if/else construct NULLIF() Return NULL

    dev.mysql.com

    위 문제는 아래 3 가지를 숙지해야 풀 수 있는 문제이다. 아직 Mysql 에 익숙하지 않아서 Discussion 과 Mysql Docs 을 탐독하면서 문제를 풀었다. (언젠가는 스스로 풀 수 있는 날이 오기를!)

    - Sub Query 서브 쿼리
    - Control Flow Function 제어문
    - Variable Assignment 변수 처리

     

    1. 문제 해석

    Pivot the Occupation column in OCCUPATIONS 
    so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation.
    The output column headers should be Doctor, Professor, Singer, and Actor, respectively.

    pivot table : Aggregation 목적, Group by 필요
    - column (열), row (행), statistic data (데이터) 구성
    - column: Occupation (Doctor, Professor, Singer, Actor)
    - row: Name (asc)

    pivot table example referred at Wikipedia

     

    2. 쿼리문 구조 결정

    column 은 각 Occupations 직업 인데... row 는 뭐지?? 
    sample output 을 뜯어보았을 때 여기서 막혔다. 

          Doctor   Professor   Singer   Actor
       Jenny    Ashley     Meera  Jane
       Samantha Christeen  Priya  Julia
       NULL     Ketty      NULL   Maria

    Q1. group by 기준 ?
    Q2. pivot 은 통계 데이터라고 했는데 sum, avg 대상이 아닌데? 

          Doctor   Professor   Singer   Actor
       Jenny    Ashley     Meera  Jane
       Samantha Christeen  Priya  Julia
       NULL     Ketty      NULL   Maria

    A1, A2 는 생각해보고 [더보기] 클릭!

    더보기

    A1. row 1 행은 각 직업마다 첫번째로 오는 대상들: rowNum 변수 관리 필요
    A2. 통계데이터 중 데이터값 자체를 내주는 함수: group by rowNum / 각 rowNum 중에서 min/max


    rowNum가 있는 temp 임시테이블을 만들어야겠다!
    데이터의 행마다 temp 임시테이블 한 행씩 차례차례 채우고, (SubQuery로 묶고)
    - 
    rowNum, Doctor, Professor, Singer, Actor 

    temp 임시테이블 에서 rowNum 기준으로 group by 집계! 

     

    3. temp 테이블 작성

    rowNum 에 대해 User-Defined VariablesVariable Assignment 필요. 
    Control Flow Functions 를 활용하여 temp 테이블 부터 차근히 작성한다. (단번에 집계를 갈 수 있는 실력이 안되므로!)

    set @r1=0, @r2=0, @r3=0, @r4=0;
    
    select
        case when occupation = 'Doctor' then (@r1:=@r1+1)
        	 when occupation = 'Professor' then (@r2:=@r2+1)
             when occupation = 'Singer' then (@r3:=@r3+1)
             when occupation = 'Actor' then (@r4:=@r4+1) end as rowNum,
        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
    from OCCUPATIONS
    order by Name;

     

    4. 최종쿼리문

    Group by / 피벗결과 min 으로 pivot 한다.

    set @r1=0, @r2=0, @r3=0, @r4=0;
    
    select min(Doctor), min(Professor), min(Singer), min(Actor) from (
    	select
        	case when occupation = 'Doctor' then (@r1:=@r1+1)
        		 when occupation = 'Professor' then (@r2:=@r2+1)
             	when occupation = 'Singer' then (@r3:=@r3+1)
             	when occupation = 'Actor' then (@r4:=@r4+1) end as rowNum,
        	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
    	from OCCUPATIONS
        order by Name) as temp
    group by rowNum;

     

    - 영어로 읽으면 설읽게되서, 곱씹으면서 쓴 문제풀이 끝 -

    댓글

Designed by Tistory.