当前位置: 代码迷 >> Oracle开发 >> 请问一个行转列的有关问题,期待高手
  详细解决方案

请问一个行转列的有关问题,期待高手

热度:106   发布时间:2016-04-24 06:42:07.0
请教一个行转列的问题,期待高手
原表 
a b c d          e     f
-1 1 0 100   x1 25
-1 1 0 100  x2 31
-1 1 0 100  x3 26
-1 1 1 200  x1 14
-1 1 1 200  x2 43
-1 1 1 200  x3 46
-1 1 2 300  x1 38
-1 1 2 300  x2 29
-1 1 2 300  x3 30

转换后

a b d1    d2    d3         e  f1    f2   f3
-1 1 100  200 300  x1 25 14 38
-1 1 100  200 300  x2 31 43 29
-1 1 100  200 300  x3 26 46 30

期待大神帮忙

------解决方案--------------------
引用:
原表 
a b c d          e     f
-1 1 0 100   x1 25
-1 1 0 100  x2 31
-1 1 0 100  x3 26
-1 1 1 200  x1 14
-1 1 1 200  x2 43
-1 1 1 200  x3 46
-1 1 2 300  x1 38
-1 1 2 300  x2 29
-1 1 2 300  x3 30

转换后

a b d1    d2    d3         e  f1    f2   f3
-1 1 100  200 300  x1 25 14 38
-1 1 100  200 300  x2 31 43 29
-1 1 100  200 300  x3 26 46 30

期待大神帮忙

with t as
(select  -1 a, 1 b, 0 c, 100 d,'x1' e,25 f from dual union all
select  -1, 1, 0, 100,  'x2', 31 from dual union all
select  -1, 1, 0, 100,  'x3', 26 from dual union all
select  -1, 1, 1, 200,  'x1', 14 from dual union all
select  -1, 1, 1, 200,  'x2', 43 from dual union all
select  -1, 1, 1, 200,  'x3', 46 from dual union all
select  -1, 1, 2, 300,  'x1', 38 from dual union all
select  -1, 1, 2, 300,  'x2', 29 from dual union all
select  -1, 1, 2, 300,  'x3', 30 from dual)
 select a,
        b,
        max(decode(d, 100, d)) d1,
        max(decode(d, 200, d)) d2,
        max(decode(d, 300, d)) d3,
        e,
        max(decode(d, 100, f)) f1,
        max(decode(d, 200, f)) f2,
        max(decode(d, 300, f)) f3
   from t
  group by a, b, e
  相关解决方案