안녕하세요 어제에 이어 오늘도 피벗과 관련한 질문을 하고자 합니다.
아래와 같은 구조가 있습니다.
Parent Name | Class | Item | Category | task | Time | Loc | Dept |
R | OCR | A | F9 | fab | 0 | KI | CK |
R | OCR | B | F9 | bmac | 30 | E1 | MA |
R | OCR | B | F9 | fab | 0 | E1 | TI |
R | OCR | K | F9 | amec | 178 | K1 | PK1 |
R | OCR | K | F9 | fab | 0 | K3 | SKD |
R | OCR | C | F9 | amec | 31 | K1 | PK1 |
R | OCR | C | F9 | bmac | 0 | K1 | HEG |
R | OCR | C | F9 | ahyd | 68 | K1 | PK1 |
R | OCR | C | F9 | ahyd | 0 | K1 | BIN |
R | OCR | C | F9 | aele | 1957 | K1 | PK1 |
R | OCR | C | F9 | fab | 0 | K2 | JEG |
R | OCR | C | F9 | fab | 0 | K3 | SKD |
R | OCR | KB | F9 | amec | 84 | K1 | PK1 |
R | OCR | KB | F9 | fab | 0 | K1 | CK |
R | OCR | P | F9 | amec | 49 | K1 | PK1 |
R | OCR | P | F9 | bmac | 0 | K2 | HEG |
R | OCR | P | F9 | fab | 378 | E1 | BMA |
R | OCR | W | F9 | bmac | 54 | E1 | MA |
R | OCR | W | F9 | fab | 0 | E1 | TI |
위의 테이블을 Task 별로 Loc, Dept, Time을 보일 수 있도록 피벗을 통해 아래와 같이 표시하고 싶습니다.
Task 들은 4가지 외에 추가되는 부분은 없으나 C Item 처럼 하나의 item에 대해 task가 각각 Loc, time, dept에 2개 값을 가질 수 있습니다.
Parent Name | Class | Item | Category | fab | bmac | amec | ahyd | aele | ||||||||||
Time | Loc | Dept | Time | Loc | Dept | Time | Loc | Dept | Time | Loc | Dept | Time | Loc | Dept | ||||
R | OCR | A | F9 | 0 | KI | CK | ||||||||||||
R | OCR | B | F9 | 0 | E1 | TI | 30 | E1 | MA | |||||||||
R | OCR | C | F9 | 0 | K2 | JEG | 0 | K1 | HEG | 31 | K1 | PK1 | 0 | K1 | BIN | 1957 | K1 | PK1 |
0 | K3 | SKD | 68 | K1 | PK1 | |||||||||||||
R | OCR | K | F9 | 0 | K3 | SKD | 178 | K1 | PK1 | |||||||||
R | OCR | KB | F9 | 0 | K1 | CK | 84 | K1 | PK1 | |||||||||
R | OCR | P | F9 | 378 | E1 | BMA | 0 | K2 | HEG | 49 | K1 | PK1 | ||||||
R | OCR | W | F9 | 0 | E1 | TI | 54 | E1 | MA |
사용중인 Database는 9i입니다.
고수분들의 많은 도움 바랍니다.
이틀째 이걸로 삽질 중인데 결과가 쉽게 나오질 않네요 ㅠㅠ
select t.parentnm, t.clss, t.itm, t.ctgr, case when t.tsk = 'fab' then tm end as fab_time, case when t.tsk = 'fab' then loc end as fab_loc, case when t.tsk = 'fab' then dept end as fab_dept, case when t.tsk = 'bmac' then tm end as bmac_time, case when t.tsk = 'bmac' then loc end as bmac_loc, case when t.tsk = 'bmac' then dept end as bmac_dept, case when t.tsk = 'amec' then tm end as amec_time, case when t.tsk = 'amec' then loc end as amec_loc, case when t.tsk = 'amec' then dept end as amec_dept, case when t.tsk = 'ahyd' then tm end as ahyd_time, case when t.tsk = 'ahyd' then loc end as ahyd_loc, case when t.tsk = 'ahyd' then dept end as ahyd_dept, case when t.tsk = 'aele' then tm end as aele_time, case when t.tsk = 'aele' then loc end as aele_loc, case when t.tsk = 'aele' then dept end as aele_dept from ttt t
이런식으로 하면 안될까요? task 5가지 종류가 늘어나지 않는다면....
SELECT parentnm, clss, itm, ctgr , MIN(DECODE(tsk, 'fab' , tm )) AS fab_time , MIN(DECODE(tsk, 'fab' , loc )) AS fab_loc , MIN(DECODE(tsk, 'fab' , dept)) AS fab_dept , MIN(DECODE(tsk, 'bmac', tm )) AS bmac_time , MIN(DECODE(tsk, 'bmac', loc )) AS bmac_loc , MIN(DECODE(tsk, 'bmac', dept)) AS bmac_dept , MIN(DECODE(tsk, 'amec', tm )) AS amec_time , MIN(DECODE(tsk, 'amec', loc )) AS amec_loc , MIN(DECODE(tsk, 'amec', dept)) AS amec_dept , MIN(DECODE(tsk, 'ahyd', tm )) AS ahyd_time , MIN(DECODE(tsk, 'ahyd', loc )) AS ahyd_loc , MIN(DECODE(tsk, 'ahyd', dept)) AS ahyd_dept , MIN(DECODE(tsk, 'aele', tm )) AS aele_time , MIN(DECODE(tsk, 'aele', loc )) AS aele_loc , MIN(DECODE(tsk, 'aele', dept)) AS aele_dept FROM ttt GROUP BY parentnm, clss, itm, ctgr ;
SELECT parentnm, clss, itm, ctgr , rn , MIN(DECODE(tsk, 'fab' , tm )) AS fab_time , MIN(DECODE(tsk, 'fab' , loc )) AS fab_loc , MIN(DECODE(tsk, 'fab' , dept)) AS fab_dept , MIN(DECODE(tsk, 'bmac', tm )) AS bmac_time , MIN(DECODE(tsk, 'bmac', loc )) AS bmac_loc , MIN(DECODE(tsk, 'bmac', dept)) AS bmac_dept , MIN(DECODE(tsk, 'amec', tm )) AS amec_time , MIN(DECODE(tsk, 'amec', loc )) AS amec_loc , MIN(DECODE(tsk, 'amec', dept)) AS amec_dept , MIN(DECODE(tsk, 'ahyd', tm )) AS ahyd_time , MIN(DECODE(tsk, 'ahyd', loc )) AS ahyd_loc , MIN(DECODE(tsk, 'ahyd', dept)) AS ahyd_dept , MIN(DECODE(tsk, 'aele', tm )) AS aele_time , MIN(DECODE(tsk, 'aele', loc )) AS aele_loc , MIN(DECODE(tsk, 'aele', dept)) AS aele_dept FROM (SELECT parentnm, clss, itm, ctgr , tsk, tm, loc, dept , ROW_NUMBER() OVER( PARTITION BY parentnm, clss, itm, ctgr, tsk ORDER BY 1 -- 정렬기준이 명확하면 좋음, 일단은 숫자 1 로 처리 -- ) rn FROM ttt ) GROUP BY parentnm, clss, itm, ctgr, rn ;