행을 열로 변환하는 문제 0 3 2,629

by 지누기 [2013.09.07 14:13:06]


안녕하세요, 오라클 공부하다가 행을 열로 변환하는 문제가 있어, 이리저리 검색을 해봤는데

어떻게 처리해야할지 감이 오질 않아서 이렇게 여쭤봅니다.


with t(dt, a, b, c, d, e, f) as
(
  select 1101 ,1 ,2 ,3 ,4 ,5 ,6  from dual union all
  select 1102, 2, 3, 4, 5, 6, 1 from dual union all
  select 1103, 3, 4, 5, 6, 1, 2 from dual union all
  select 1104, 4, 5, 6, 1, 2, 3 from dual union all
  select 1105, 5, 6, 1, 2, 3, 4 from dual 
)

다음과 같은 데이터가 있을 때,

DT A B C D E F
1101 1 2 3 4 5 6
1102 2 3 4 5 6 1
1103 3 4 5 6 1 2
1104 4 5 6 1 2 3
1105 5 6 1 2 3 4

위의 방식으로 나오는 데이터를

Character 1101 1102 1103 1104 1105
A 1 2 3 4 5
B 2 3 4 5 6
C 3 4 5 6 1
D 4 5 6 1 2
E 5 6 1 2 3
F 6 1 2 3 4

위와 같이 변경을 하고 싶습니다. 이 방식이 쿼리로 가능할지, 어떤 식으로 풀어나가야할지 조언 좀 부탁드리겠습니다.

감사합니다.

by 지누기 [2013.09.07 15:21:57]
with t(no, dt, a, b, c, d, e, f) as
(
select rownum as no, x.* from(
  select 1101 ,1 ,2 ,3 ,4 ,5 ,6  from dual union all
  select 1102, 2, 3, 4, 5, 6, 1 from dual union all
  select 1103, 3, 4, 5, 6, 1, 2 from dual union all
  select 1104, 4, 5, 6, 1, 2, 3 from dual union all
  select 1105, 5, 6, 1, 2, 3, 4 from dual )x
)
select  'a' as Character
          ,max(decode(no, '1', a)) "1101"
          ,max(decode(no, '2', a)) "1102"
          ,max(decode(no, '3', a)) "1103"
          ,max(decode(no, '4', a)) "1104"
          ,max(decode(no, '5', a)) "1105"
from t
 union all
select  'b' as Character
          ,max(decode(no, '1', b)) "1101"
          ,max(decode(no, '2', b)) "1102"
          ,max(decode(no, '3', b)) "1103"
          ,max(decode(no, '4', b)) "1104"
          ,max(decode(no, '5', b)) "1105"
from t
 union all
select  'c' as Character
          ,max(decode(no, '1', c)) "1101"
          ,max(decode(no, '2', c)) "1102"
          ,max(decode(no, '3', c)) "1103"
          ,max(decode(no, '4', c)) "1104"
          ,max(decode(no, '5', c)) "1105"
from t
 union all
select  'd' as Character
          ,max(decode(no, '1', d)) "1101"
          ,max(decode(no, '2', d)) "1102"
          ,max(decode(no, '3', d)) "1103"
          ,max(decode(no, '4', d)) "1104"
          ,max(decode(no, '5', d)) "1105"
from t
 union all
select  'e' as Character
          ,max(decode(no, '1', e)) "1101"
          ,max(decode(no, '2', e)) "1102"
          ,max(decode(no, '3', e)) "1103"
          ,max(decode(no, '4', e)) "1104"
          ,max(decode(no, '5', e)) "1105"
from t
 union all
select  'f' as Character
          ,max(decode(no, '1', f)) "1101"
          ,max(decode(no, '2', f)) "1102"
          ,max(decode(no, '3', f)) "1103"
          ,max(decode(no, '4', f)) "1104"
          ,max(decode(no, '5', f)) "1105"
from t

다음과 같이 무식하게는 짜 봤습니다;; pivot 이나 connect by level 등 함수를 사용하신 예제들을 
계속 훑어봤는데, 좀 더 세련되게 짜고 싶어요 ㅠㅠ 조언 좀 부탁드릴께요

by YuSin [2013.09.08 12:15:59]
 
with t(dt, a, b, c, d, e, f) as
(
 select 1101 ,1 ,2 ,3 ,4 ,5 ,6 from dual union all
 select 1102, 2, 3, 4, 5, 6, 1 from dual union all
 select 1103, 3, 4, 5, 6, 1, 2 from dual union all
 select 1104, 4, 5, 6, 1, 2, 3 from dual union all
 select 1105, 5, 6, 1, 2, 3, 4 from dual 
)
select Character
  , max(case when dt = 1101 then value end) "1101"
  , max(case when dt = 1102 then value end) "1102"
  , max(case when dt = 1103 then value end) "1103"
  , max(case when dt = 1104 then value end) "1104"
  , max(case when dt = 1105 then value end) "1105"
from (
  select dt
   , decode(no, 1, 'A', 2, 'B', 3, 'C', 4, 'D', 5, 'E', 6, 'F') Character
   , decode(no, 1, a, 2, b, 3, c, 4, d, 5, e, 6, f) value
  from t
   , (select level no from dual connect by level <= 10)
  where no <= 6
  order by 1,2
  )
group by Character
order by Character;




with t(dt, a, b, c, d, e, f) as
(
 select 1101 ,1 ,2 ,3 ,4 ,5 ,6 from dual union all
 select 1102, 2, 3, 4, 5, 6, 1 from dual union all
 select 1103, 3, 4, 5, 6, 1, 2 from dual union all
 select 1104, 4, 5, 6, 1, 2, 3 from dual union all
 select 1105, 5, 6, 1, 2, 3, 4 from dual 
)
select *
from (
  select * 
  from t
  unpivot include nulls
  (val for Character in (a,b,c,d,e,f))
  )
pivot (max(val) for dt in (1101,1102,1103,1104,1105))
order by Character;

by 지누기 [2013.09.08 14:56:51]
정말 감사합니다! 덕분에 많은 도움이 되었어요~^^
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입