oracle实现分组字符串相加

用oracle实现 分组字符串相加如下图所示的查询

create table test
(
       NO nvarchar2(10),
       Q nvarchar2(10)
)
insert into test values('001','n1');
insert into test values('001','n2');
insert into test values('001','n3');
insert into test values('001','n4');
insert into test values('001','n5');
insert into test values('001','n6');
insert into test values('002','m1');
insert into test values('003','t1');
insert into test values('003','t2');

select   no,max(sys_connect_by_path(q,';'))   result    
  from   (select   no,q,  
                            (row_number()   over(order   by   no,q   desc)    
                            +   dense_rank()   over(order   by   no))   rn,    
                            max(q)   over(partition   by   no)   qs  
              from   test  
  )  
    start   with   q   =   qs  
    connect   by   rn-1   =   prior   rn  
    group   by   no

posted @ 2010-05-18 08:24  XGU_Winner  阅读(935)  评论(0编辑  收藏  举报