select 字段1,max(sys_connect_by_path(字段2||字段3,' ')) result
from (select 字段1,字段2,字段3,
             (row_number() over(order by 字段1,字段2,字段3desc)
             + dense_rank() over(order by 字段1)) rn,
             max(字段2) over(partition by 字段1) qs
      from table)
 start with id = qs
 connect by rn-1 = prior rn
 group by 字段1

解决方案 »

  1.   

    呵呵,竟然还是用的那个SQL,就连RN,QS都没变,哈哈!
      

  2.   

    select col1,max(sys_connect_by_path(col2 || col3,' ')) result
      from (
           select col1,col2,col3,
                  (row_number() over(order by col1,col2,col3 desc) + dense_rank() over(order by col1)) rn,
                  (max(col2) over(partition by col1)) qs 
             from test
           )
      start with id = qs
      connect by rn-1 = prior rn
      group by col1
    /
    SQL> /
      start with id = qs
                 *
    ERROR 位于第 8 行:
    ORA-00904: "ID": 无效的标识符
    请问这个id是从哪来的啊?另外能解释一下其中使用道德函数吗?谢谢
      

  3.   

    col2就是ID,但是这样达不到你要的效果,这样只能查到2个字段。
      

  4.   

    如果字段3是NUMBER型的话,就不用这么麻烦了。Connected to Oracle9i Enterprise Edition Release 9.2.0.1.0 
    Connected as SYSSQL> 
    SQL> create table test(col1 varchar2(10),col2 number,col3 number);Table createdSQL> insert into test select 'a',1,11 from dual;1 row insertedSQL> insert into test select 'a',2,22 from dual;1 row insertedSQL> insert into test select 'a',3,13 from dual;1 row insertedSQL> insert into test select 'b',1,1 from dual;1 row insertedSQL> insert into test select 'b',2,3 from dual;1 row insertedSQL> commit;Commit completeSQL> select * from test;COL1             COL2       COL3
    ---------- ---------- ----------
    a                   1         11
    a                   2         22
    a                   3         13
    b                   1          1
    b                   2          3SQL> 
    SQL> 
    SQL> select col1,sum(decode(col2,1,col2)) a,sum(decode(col2,1,col3)) b,
      2  sum(decode(col2,2,col2)) c,sum(decode(col2,2,col3)) d,
      3  sum(decode(col2,3,col2)) e,sum(decode(col2,3,col3)) f
      4  from test group by col1;COL1                A          B          C          D          E          F
    ---------- ---------- ---------- ---------- ---------- ---------- ----------
    a                   1         11          2         22          3         13
    b                   1          1          2          3            SQL>
      

  5.   

    connect by是ORACLE10i中的新特性。所以楼主报错……ERROR 位于第 8 行:
    ORA-00904: "ID": 无效的标识符
      

  6.   

    有誰能否幫忙解釋一下第一樓: yearlist(泥巴) 所給的代碼是什麼意思嗎?能否解釋一下其中的函數和意義呀?
      

  7.   

    connect by是ORACLE10i中的新特性
    --------------------------------------是9i的!
    建议楼主买本书学一下,我一下也不好说清楚的.就是有关层次查询!
    把ID改为COL2.应该可以显示出一点效果,但它是用2个字段来显示的.所以说还是达不到楼主的要求,建议用过程做一下.