하나의 레코드를 여러개로 만드는 방법 1 5 3,721

by 박준 [SQL Query] 열을행으로 UNPIVOT [2012.03.13 16:18:28]


안녕하세요.
다름이 아니라 결과가 하나로 나오는 쿼리를 여러개의 쿼리로 바꾸고 싶습니다.

조언 좀 부탁드리겠습니다.

9i 입니다.

select YYMM, GOODS, BASE_GUBUN, BASE_AMT, USE_GUBUN, USE_AMT, UTIL_GUBUN, AMT_UTIL
from a_table


YYMM GOODS BASE_GUBUN BASE_AMT USE_GUBUN USE_AMT UTIL_GUBUN AMT_UTIL
201103 D07 기본료 9240 장비임대료 4400 부가서비스 0
201103 D44 기본료 0 장비임대료 0 부가서비스 1320
201103 406 기본료 14570 장비임대료 0 부가서비스 0
201104 D07 기본료 9240 장비임대료 4400 부가서비스 0

위의 내용을

YYMM GOODS GUBUN AMT
201103 D07 기본료 9240
201103 D44 기본료 0
201103 406 기본료 14570
201104 D07 기본료 9240
201103 D07 장비임대료 4400
201103 D44 장비임대료 0
201103 406 장비임대료 0
201104 D07 장비임대료 4400
201103 D07 부가서비스 0
201103 D44 부가서비스 1320
201103 406 부가서비스 0
201104 D07 부가서비스 0


이렇게 변경하는 쿼리를 아무리 찾아봐도 비슷한것이 없네요.

부탁드리겠습니다.

감사합니다.
by 마농 [2012.03.13 16:59:36]
11g라면 Pivot 기능을 사용해 보세요.
WITH t(yymm, goods, base_gubun, base_amt, use_gubun, use_amt, util_gubun, util_amt) AS
(
SELECT '201103', 'D07', '기본료',  9240, '장비임대료', 4400, '부가서비스',    0 FROM dual
UNION ALL SELECT '201103', 'D44', '기본료',     0, '장비임대료',    0, '부가서비스', 1320 FROM dual
UNION ALL SELECT '201103', '406', '기본료', 14570, '장비임대료',    0, '부가서비스',    0 FROM dual
UNION ALL SELECT '201104', 'D07', '기본료',  9240, '장비임대료', 4400, '부가서비스',    0 FROM dual
)
SELECT *
  FROM t
 UNPIVOT ((gubun_nm, amt)
   FOR gubun_cd IN
   ( (base_gubun, base_amt) AS 1
   , ( use_gubun, use_amt) AS 2
   , (util_gubun, util_amt) AS 3
   )
 )
 ORDER BY gubun_cd, yymm, goods
;

by 박준 [2012.03.13 17:07:43]

애석하게도.. 9i  입니다.

by 마농 [2012.03.13 17:21:19]
SELECT yymm
     , goods
     , DECODE(lv, 1, base_gubun, 2, use_gubun, 3, util_gubun) gubun
     , DECODE(lv, 1, base_amt  , 2, use_amt  , 3, util_amt  ) amt
  FROM (SELECT LEVEL lv FROM dual CONNECT BY LEVEL <= 3)
     , t
;

by 박준 [2012.03.13 17:43:32]

아.. 되네요.. 정말 감사합니다..

by 김용한 [2015.03.21 10:20:42]

감사합니다 기본이 안돼있네요 제가 ㅠㅠ

마농님 쿼리를 찬찬히 돼새겨보니 여기서 제가 알고싶던 해결점을 찾았네요 감사드립니다 ~~ ^^

 

WITH T AS (
select 1012 as YR, 0 as "01", 0 as "02", 0 as "03", 0 as "04", 0 as "05", 0 as "06", 0 as "07", 0 as "08", 0 as "09", 0 as "10", 0 as "11", 0 as "12" from dual union all
select 2011 as YR, 0 as "01", 0 as "02", 0 as "03", 0 as "04", 0 as "05", 0 as "06", 0 as "07", 0 as "08", 0 as "09", 0 as "10", 0 as "11", 385 as "12" from dual union all
select 2012 as YR, 890 as "01", 645 as "02", 833 as "03", 416 as "04", 739 as "05", 360 as "06", 552 as "07", 828 as "08", 297 as "09", 223 as "10", 348 as "11", 228 as "12" from dual union all
select 2013 as YR, 1332 as "01", 680 as "02", 841 as "03", 385 as "04", 327 as "05", 781 as "06", 459 as "07", 829 as "08", 207 as "09", 135 as "10", 190 as "11", 466 as "12" from dual union all
select 2014 as YR, 261 as "01", 432 as "02", 895 as "03", 347 as "04", 396 as "05", 471 as "06", 529 as "07", 495 as "08", 159 as "09", 173 as "10", 167 as "11", 356 as "12" from dual union all
select 2015 as YR, 439 as "01", 266 as "02", 361 as "03", 0 as "04", 0 as "05", 0 as "06", 0 as "07", 0 as "08", 0 as "09", 0 as "10", 0 as "11", 0 as "12" from dual
)
select yr
     , lv as mm
     , decode(lv,1, "01"
                ,2, "02"
                ,3, "03"
                ,4, "04"
                ,5, "05"
                ,6, "06"
                ,7, "07"
                ,8, "08"
                ,9, "09"
                ,10,"10"
                ,11,"11"
                ,12,"12"
                ,0) as VAL
  from (select level lv from dual connect by level<=12),t 
 where t.yr='2012'

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입