表1 字段a,b a b
X 1
X 2
X 3
Y 1
Y 2
Z 3 表2 字段c,d,e,f……
c d e f
X 1 2 3
Y 1 2 NULL
Z 3 NULL NULL
求一sql实现表1到表2的转换,谢谢了!

解决方案 »

  1.   


    with tb as(
    select 'X' a,1 b from dual union all
    select 'X', 2 from dual union all
    select 'X', 3 from dual union all
    select 'Y', 1 from dual union all
    select 'Y', 2 from dual union all
    select 'Z', 3 from dual)
    --以上为提供数据的语句
    select  a,max(decode(b,1,b,null)) d,
    max(decode(b,2,b,null)) e,
    max(decode(b,3,b,null)) f
    from tb
    group by a
    order by a
    A          D          E          F
    - ---------- ---------- ----------
    X          1          2          3
    Y          1          2
    Z                                3