有辦法做到如下的結果嗎?name    location
---------------------
b       1
a       1
a       2
a       3
b       3
name    location
-------------------
a       1,2,3
b       1,3

解决方案 »

  1.   

    select name,wm_concat(location)location from tt
      

  2.   

    select name,wmsys.wm_concat(location) from table group by name;
      

  3.   

    上面是10g以上的
    9i上的写法:
    select name,substr(max(sys_connect_by_path(location,',')),2)location
      from (select tt.*,row_number()over(partition by name order by rownum)rn from tt)
    start with rn=1
    connect by prior name=name and prior rn=rn-1
    group by name
      

  4.   


    我的是9i版,只能用這個方法,的確是可行,
    但如果我要結合另一個table要怎麼寫呢?
      

  5.   


    幫忙一下!!!T1:                     T2:
    id  name    location        id    qty
    ------------------     ----------------- 
    1    b      1               1      2
    2    a      1               2      1
    3    a      2               3      5
    4    a      3               4      0
    5    b      3               5      1
    name    location    avg(qty)
    -----------------------------
     a      1,2,3         3
     b      1,3           1.5
      

  6.   

    select name,substr(max(sys_connect_by_path(location,',')),2)location,avg(qty) 
      from (select t1.*,t2.qty,row_number()over(partition by a.name order by rownum)rn 
        from t1,t2 where t1.id=t2.id) 
    start with rn=1 
    connect by prior name=name and prior rn=rn-1 
    group by name
      

  7.   


    結果好像不是這樣,
    因為我舉的例子是簡單版的,
    但我參考改寫的結果是
    AVG的值不正確,好像抓到的是最後一個值,
    為什麼會這樣呢?
      

  8.   

    select name,substr(max(sys_connect_by_path(location,',')),2)location,avg(qty) 
      from (select t1.*,t2.qty,row_number()over(partition by t1.name order by rownum)rn 
        from t1,t2 where t1.id=t2.id) 
    start with rn=1 
    connect by prior name=name and prior rn=rn-1 
    group by name
    语句没有问题,除了子查询里边row_number()后面的a.name要改成t1.name
    你的例子结果里 avg(qty) 对应a记录的那个值是3,实际上是2吧
    你说的好像抓到的是最后一个值是什么意思
    是不是你改完后表没连接好
      

  9.   


    請問若t2 的id沒有排序,
    結果會是一樣的嗎?
    需要改寫成
    t1.id=t2.id (+)
    我原來的資料庫資料很多,
    不知是否我舉的例子有問題!!!
      

  10.   

    id没有排序是什么意思..
    t2的id和t1的id必须对应,否则就没有联系了
    若t1有些id在t2里找不到,这样的记录你也要查出来的话
    就用t1.id=t2.id(+)
    avg(qty)可能也得改下才能满足你的需要avg(nvl(qty,0))
      

  11.   

    我在PLSQL里测试,为什么会提示WM_CONCAT无效的标识符呢?