日期:2014-05-17  浏览次数:20963 次

一个查询语句。解决马上结贴。
有一个主表 TableA
一个子表 TableB

TableA 列:
GroupA

TableB 列:
GroupA  
Message

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

------解决方案--------------------
--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
------解决方案--------------------
关联的话
SQL code

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

------解决方案--------------------
探讨

关联的话
SQL code

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

------解决方案--------------------
上面 符号写反了 
SQL code

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