有一个主表 TableA
一个子表  TableBTableA  列:
GroupATableB  列:
GroupA  
Message数据我写2个,表TableA
ItemA
表TableB
ItemA, MessageA
ItemA, MessageB
现在通过主表关联子表,我想得到如下结果,
ItemA       MessageA,MessageB
就是子表中的内容,分组后,用,分隔得到结果。   

解决方案 »

  1.   

    --wm_concat()函数用法
    with tb as (
    select '王' name,'数学' course,  11  type,  '2011-11-30 18:11:00'   time from dual union all
    select '王' , '数学', 11, '2011-11-30 18:11:00' from dual union all
    select '王' , '语文', 12, '2011-10-30 18:11:00' from dual union all
    select '张' , '数学', 11, '2011-11-30 18:11:00' from  dual 
    ) select tt.name, wm_concat(tt.km), wm_concat(tt.sj)
      from (
      select name,
             to_char(course),
             to_char(t.a) as km,
             to_char(type) || '/' || to_char(to_date(time,'yyyy-mm-dd hh24:mi:ss'), 'mm.dd') as sj
        from (select name, course, count(1) a, time, type
                from tb
               group by name,course,time,type) t
    ) tt
     group by tt.name
      

  2.   

    测试如下SQL> select groupa,WMSYS.WM_CONCAT(message) from tableb group by groupa;
     
    GROUPA               WMSYS.WM_CONCAT(MESSAGE)
    -------------------- --------------------------------------------------------------------------------
    itema                messagea,messageb
     
    SQL> 
      

  3.   

    关联的话select b.groupa,WMSYS.WM_CONCAT(message) as messages from tableb b join tablea a on b.groupa = a.groupa group by b.groupa;
      

  4.   

    要是不支持 WMSYS.WM_CONCAT
    就用 sys_connect_by_path 这个来实现
      

  5.   


    select groupa,substr(max(sys_connect_by_path(message,',')),2)  as message
      from (select a.*,row_number()over(partition by groupa order by message) rn from tableb a)
      group by groupa
    start with rn=1
      connect by rn+1=prior rn and groupa=prior groupa
        order by 1
      

  6.   

    上面 符号写反了 with tableb as (
     select 'ItemA' as groupa,'MessageA' as message from dual
     union
      select 'ItemA' as groupa,'MessageB' as message from dual
       union
      select 'ItemB' as groupa,'Messagec' as message from dual
    )
    select groupa,substr(max(sys_connect_by_path(message,',')),2)  as message
      from (select a.*,row_number()over(partition by groupa order by message) rn from tableb a )
      group by groupa
    start with rn=1
      connect by rn-1=prior rn and groupa=prior groupa
        order by 1
      

  7.   

    select A.name , WMSYS.WM_CONCAT(B.msg) from A , B where A.pkid=B.pk_a group by A.name;
      

  8.   

    create function test
    (
    @a int 
    )
     
    returns int 
    as
    begin
    return  (@a+1)
    endselect   dbo.test(2)