横表有这么四列,Id是主键,
Id    code1   code2   code3
111     aaa    bbb     ccc纵表只有两列:
ID    code
111   aaa
111   bbb
111   ccc现在知道纵表,一条sql想恢复成横表,如何实现啊?
我用group by 能得到max(code),min(code)但是中间的那个值如何能取到呢,
谢谢了

解决方案 »

  1.   

    select id,max(decode(rn,1,code))code1,
      max(decode(rn,2,code))code2,
      max(decode(rn,3,code))code3
    from(select tt.*,row_number()over(partition by id order by code)rn from tt)
    group by id
      

  2.   

    with tt as(select 111 id, 'aaa' code from dual
    union all select 111,'bbb' from dual
    union all select 111,'ccc' from dual
    )
    select id,max(decode(rn,1,code))code1, 
    max(decode(rn,2,code))code2, 
    max(decode(rn,3,code))code3 
    from(select tt.*,row_number() over(partition by id order by code)rn from tt) 
    group by id;