现在有类似这样的数据:
ID TYPE QTY
01 abc 3
01 ab 2
01 a 1
02 defg 7
02 def 6
02 de 5
02 d 4我希望能以这样的形式输出:
ID TYPE QTY TYPE QTY TYPE QTY TYPE QTY
01 abc 3 ab 2 a 1
02 defg 7 def 6 de 5 d 4哪位高手能帮忙解决呀?
是Oracle的数据库,用的Orange,万分感谢~~

解决方案 »

  1.   

    with t1 as (select id,type,qty,row_number()over(partition by id order by type desc) rn  from table ),
    select id,max(decode(rn,1,type,null)),max(decode(rn,1,QTY,null)),max(decode(rn,2,type,null)),max(decode(rn,2,QTY,null)),
              max(decode(rn,3,type,null)),max(decode(rn,13,QTY,null)),max(decode(rn,4,type,null)),max(decode(rn,4,QTY,null))
      from t
     group by id
      

  2.   

    SQL> 
    SQL> with t1 as (select id,type,qty,row_number()over(partition by id order by type desc) rn  from tb_row_col )
      2  select id,max(decode(rn,1,type,null)),max(decode(rn,1,QTY,null)),max(decode(rn,2,type,null)),max(decode(rn,2,QTY,null)),
      3            max(decode(rn,3,type,null)),max(decode(rn,3,QTY,null)),max(decode(rn,4,type,null)),max(decode(rn,4,QTY,null))
      4    from t1
      5   group by id;
     
    ID         MAX(DECODE(RN,1,TYPE,NULL)) MAX(DECODE(RN,1,QTY,NULL)) MAX(DECODE(RN,2,TYPE,NULL)) MAX(DECODE(RN,2,QTY,NULL)) MAX(DECODE(RN,3,TYPE,NULL)) MAX(DECODE(RN,3,QTY,NULL)) MAX(DECODE(RN,4,TYPE,NULL)) MAX(DECODE(RN,4,QTY,NULL))
    ---------- --------------------------- -------------------------- --------------------------- -------------------------- --------------------------- -------------------------- --------------------------- --------------------------
    01         abc                                                  3 ab                                                   2 a                                                    1                             
    02         defg                                                 7 def                                                  6 de                                                   5 d                                                    4
     
    SQL> 
      

  3.   

    这个max(decode(..))我经常看见,我能问下为什么要有外层的max么?
      

  4.   

    你把max和group by 去掉,看下查询结果就知道了.
      

  5.   

    哦,明白了,必须要用group by分组。。但是max没什么具体的用处吧。。我用min有问题么?
      

  6.   

    实验中。command窗口没用过,正在熟悉,话说我点enter键进入下一行之后我怎么会上一行在修改然后继续啊
      

  7.   

    据说我试验过,在这个问题上至少是一样的,但是到底是不是一样的呢?我看别人都是max(decode()),没见过min(decode())啊。。
      

  8.   

    看你decode后其他值的取法了,按这个sql是用的null,所以你可以使用max也可以使用min,结果是一样的.
    如果我其他值转成0,那么就不能用min了,用min你会得到一堆0.
      

  9.   

    是不是在command窗口不能对整段代码复制粘贴运行啊?可以的话应该怎样复制粘贴一整段代码之后进行修改运行呢?
      

  10.   

    啊?为什么我的不行啊。。
    我复制粘贴就是这个效果
    SQL> with t1 as (select id,type,qty,row_number()over(partition by id order by type desc) rn  from tb_row_col )
      2    2  select id,min(decode(rn,1,type,0)),min(decode(rn,1,QTY,0)),min(decode(rn,2,type,0)),min(decode(rn,2,QTY,0)),
      3    3  min(decode(rn,3,type,0)),min(decode(rn,3,QTY,0)),min(decode(rn,4,type,0)),min(decode(rn,4,QTY,0))
      4    4  from t1
      5    5  group by id
      6  /
    SQL> 
    SQL> /
     
    with t1 as (select id,type,qty,row_number()over(partition by id order by type desc) rn  from tb_row_col )
      2  select id,min(decode(rn,1,type,0)),min(decode(rn,1,QTY,0)),min(decode(rn,2,type,0)),min(decode(rn,2,QTY,0)),
      3  min(decode(rn,3,type,0)),min(decode(rn,3,QTY,0)),min(decode(rn,4,type,0)),min(decode(rn,4,QTY,0))
      4  from t1
      5  group by id
     
    ORA-00928: missing SELECT keyword
     
    SQL> 
    我是在XP系统下用的plsql
      

  11.   

    我复制之后粘贴时这样的SQL>  with t1 as (select id,type,qty,row_number()over(partition by id order by type desc) rn  from tb_row_col )
      2    2  select id,min(decode(rn,1,type,0)),min(decode(rn,1,QTY,0)),min(decode(rn,2,type,0)),min(decode(rn,2,QTY,0)),
      3    3  min(decode(rn,3,type,0)),min(decode(rn,3,QTY,0)),min(decode(rn,4,type,0)),min(decode(rn,4,QTY,0))
      4    4  from t1
      5    5  group by id
      6  /SQL> /
     
    with t1 as (select id,type,qty,row_number()over(partition by id order by type desc) rn  from tb_row_col )
      2  select id,min(decode(rn,1,type,0)),min(decode(rn,1,QTY,0)),min(decode(rn,2,type,0)),min(decode(rn,2,QTY,0)),
      3  min(decode(rn,3,type,0)),min(decode(rn,3,QTY,0)),min(decode(rn,4,type,0)),min(decode(rn,4,QTY,0))
      4  from t1
      5  group by id
     
    ORA-00928: missing SELECT keyword
     删除了重复数字式这样的。SQL> with t1 as (select id,type,qty,row_number()over(partition by id order by type desc) rn  from tb_row_col )
      2    select id,min(decode(rn,1,type,0)),min(decode(rn,1,QTY,0)),min(decode(rn,2,type,0)),min(decode(rn,2,QTY,0)),
      3    min(decode(rn,3,type,0)),min(decode(rn,3,QTY,0)),min(decode(rn,4,type,0)),min(decode(rn,4,QTY,0))
      4    from t1
      5    group by id
      6  /
    Warning: connection was lost and re-established