[답변]달별로 해당사원만의 레코드를 뽑아와야 합니다. pivot을 이용해야되는.. 0 1 1,329

by 이주호 [2006.12.23 15:10:27]


테이블구조를 정확하게 몰라서

제가 임의로 구조를 정했습니다.

아래와 같이 테이블 구조를 가정해서 작성했는데

원하시는 바와 같으신지 확인하시기 바랍니다.

좋은 하루 되세요.

 

/*
column a : 사원번호
column b : 금액
column c : 월
column d : 지급코드
 - 1 : 월급
 - 2 : 상여금
 - 3 : 특별상여금
다고 가정하면
*/
select a.a
      ,sum(case when a.c=1 and a.d=1 then a.b end) 1m
      ,sum(case when a.c=2 and a.d=1 then a.b end) 2m
      ,sum(case when a.c=3 and a.d=1 then a.b end) 3m
      ,sum(case when a.c=4 and a.d=1 then a.b end) 4m
      ,sum(case when a.c=5 and a.d=1 then a.b end) 5m
      ,sum(case when a.c=6 and a.d=1 then a.b end) 6m
      ,sum(case when a.c=7 and a.d=1 then a.b end) 7m
      ,sum(case when a.c=8 and a.d=1 then a.b end) 8m

/* 9월~12월 생략 */
      ,sum(case when a.d=2 then a.b end) bonus
      ,sum(case when a.d=3 then a.b end) special_bonus
from (
       select '11111' a, 100 b, 1 c, 1 d from dual union all
       select '11112', 100, 1, 1 from dual union all
       select '11113', 100, 1, 1 from dual union all
       select '11111', 100, 2, 1 from dual union all
       select '11112', 100, 2, 1 from dual union all
       select '11113', 100, 2, 1 from dual union all
       select '11111', 100, 3, 1 from dual union all
       select '11112', 100, 3, 1 from dual union all
       select '11113', 100, 3, 1 from dual union all
       select '11111', 100, 4, 1 from dual union all
       select '11112', 100, 4, 1 from dual union all
       select '11113', 100, 4, 1 from dual union all
       select '11111', 100, 5, 1 from dual union all
       select '11112', 100, 5, 1 from dual union all
       select '11113', 100, 5, 1 from dual union all
       select '11111', 100, 6, 1 from dual union all
       select '11112', 100, 6, 1 from dual union all
       select '11113', 100, 6, 1 from dual union all
       select '11111', 100, 7, 1 from dual union all
       select '11112', 100, 7, 1 from dual union all
       select '11113', 100, 7, 1 from dual union all
       select '11111', 100, 8, 1 from dual union all
       select '11112', 100, 8, 1 from dual union all
       select '11113', 100, 8, 1 from dual union all
       select '11111', 100, 2, 2 from dual union all
       select '11112', 100, 2, 2 from dual union all
       select '11113', 100, 2, 2 from dual union all
       select '11111', 100, 7, 2 from dual union all
       select '11112', 100, 7, 2 from dual union all
       select '11113', 100, 7, 2 from dual union all
       select '11111', 100, 3, 3 from dual union all
       select '11112', 100, 3, 3 from dual union all
       select '11113', 100, 3, 3 from dual union all
       select '11111', 100, 8, 3 from dual union all
       select '11112', 100, 8, 3 from dual union all
       select '11113', 100, 8, 3 from dual
     ) a
group by a.a

by 손님 [2006.12.26 00:00:00]
감사합니다 ㅠㅠ 크리스마스에 작업해서 해결했는데 이게 더 좋아보이네요 -_-;;; 뜯어 고칠까...
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입