多表查询,组合结果问题
2个表
table1 表结构 id,name
table2 表结构 id,title,ct_pointtable1数据大概如下
[id],[name]
1  人员1
2  人员2
3  人员3table2数据大概如下
[id],[title],[ct_point]
1       A       100
2       A        35
2       B        40
2       C        45
3       C        11
3       B        22查询结果为
[id],[name],[title],[ct_point]
1    人员1    A          100
2    人员2    A,B,C      35,40,45
3    人员3    C,B        11,22也就是当 table1数据在 table2里有多个对应数据时
把他们已字符串形式加起来,中间用 , 来间隔
这个大概怎么实现啊

解决方案 »

  1.   

    select a.id,a.name,wm_concat(b.title),wm_concat(b.ct_point)
    from table1 a,table2 b
    where a.id=b.id
    group by a.id,a.name
      

  2.   

    select id,max(substr(sys_connect_by_path(name,','),2)) from (select t.*,row_number() over(partition by id order by id) rn from tab1,tab2 where tab1.id=tab2.id(+) t) where connect_by_isleaf=1 start with rn =1 connect by rn =prior rn+1 and id =prior id
      

  3.   

    谢谢
    不过出现了问题
    正确的应该是
    2    人员2    A,B,C      35,40,45 
    搜到的结果是
    2    人员2    A,B,C      35,45,40 等于是 B和C的值位置变了
    应该是排序的问题吧
      

  4.   

    wm_concat不负责排序
    如果有排序上的要求,改成sys_connect_by_path
    select a.id,a.name,wm_concat(b.title),wm_concat(b.ct_point) 
    from table1 a,table2 b 
    where a.id=b.id 
    group by a.id,a.name select a.id,a.name,
      substr(max(sys_connect_by_path(b.title,',')),2)title,
      substr(max(sys_connect_by_path(b.ct_point,',')),2)ct_point
    from table1 a,(
      select table2.*,row_number()over(partition by id order by title)rn
      from table2)b
    where a.id=b.id
    start with b.rn=1
    connect by prior b.rn=b.rn-1 and prior b.id=b.id
    group by a.id,a.name