有一张表:no name    text
1  a       hello
2  a       world
3  a       haha
4  b       ccc
5  b       ddd我想根据name字段,如果同名就显示:a  helloworldhaha
b  ccdd怎么用一条SQL语句(不用store procedure)来做到?

解决方案 »

  1.   


    with t as(
    select 1 id,'a' name,'hello' text from dual
    union all
    select 2,'a','world' from dual
    union all
    select 3,'a','haha' from dual
    union all
    select 4,'b','ccc' from dual
    union all
    select 5,'b','ddd' from dual
    )select 
        name,replace(wm_concat(text),',','')
     from t 
        group by name;
    NAME REPLACE(WM_CONCAT(TEXT),',',''
    ---- --------------------------------------------------------------------------------
    a    helloworldhaha
    b    cccddd
     
      

  2.   

    这个有点太迁强了吧,如果我这个表里的行数有3000条,而且name不一定只有a,b,c可能还有其它的名字呢?都是HardCode?
      

  3.   

    没有hardcode,那几条记录只是试例。你可以用你表的数据试试
      

  4.   


    如果我的表名叫sp,你的那段SQL因该怎么写啊?还望指教。
      

  5.   

    但现在有一个问题,如果这个text字段里的东西都很长,上千条记录,一旦wm_concat后就会报“字符缓冲区太小”,设到了极限32767还是超长。
    不知道还有没有其它办法来拼这个字符串呢?
      

  6.   

    select 表。name,replace(wm_concat(表。text),',','')  from 表 group by 表。name;
      

  7.   

    如果是Oracle 11g R2,可用listagg,就可不用Replace去转换了
     select 表.name,listagg(表.text),'') within group (order by 表.name) from 表 group by 表.name;