例如 1:
              项目编号         字段a         字段b         字段c 
-------------------------------------------- 
记录1       1                       abc             xyz           1234   
记录2       1                       abc             xyz           8888   结果是: 
              项目编号         字段a         字段b         字段c 
-------------------------------------------- 
记录1       1                       abc             xyz           12348888   
例如 1:              项目编号         字段a         字段b         字段c 
-------------------------------------------- 
记录1       1                       abc             xyz           1234   
记录2       1                       abc             xyz           8888   
记录3       1                       abc             xyz           883388   
结果是: 
              项目编号         字段a         字段b         字段c 
-------------------------------------------- 
记录1       1                       abc             xyz           12348888883388   
数据相同有很多,但是 项目编号,字段a ,字段b  一样,需要拼接最后一个

解决方案 »

  1.   


    select 项目编号,字段a,字段b,replace(vm_concat(字段c),',') as 字段c
    from table
    group by 项目编号,字段a,字段b
      

  2.   

    SQL> with t as
      2   (select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 1234 字段3
      3      from dual
      4    union all
      5    select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 8888 字段3
      6      from dual
      7    union all
      8    select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 8833888 字段3
      9      from dual
     10    union all
     11    select 2 项目编号, 'def' 字段1, 'mno' 字段2, 987 字段3
     12      from dual
     13    union all
     14    select 2 项目编号, 'def' 字段1, 'mno' 字段2, 321 字段3 from dual)
     15  select 项目编号, 字段1, 字段2, wm_concat(字段3) 字段3
     16    from t
     17   group by 项目编号, 字段1, 字段2;
     
          项目编号 字段1 字段2 字段3
    ---------- ----- ----- --------------------------------------------------------------------------------
             1 abc   xyz   1234,8888,8833888
             2 def   mno   987,321
     
    SQL>
      

  3.   

    SQL> with t as
      2   (select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 1234 字段3
      3      from dual
      4    union all
      5    select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 8888 字段3
      6      from dual
      7    union all
      8    select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 8833888 字段3
      9      from dual
     10    union all
     11    select 2 项目编号, 'def' 字段1, 'mno' 字段2, 987 字段3
     12      from dual
     13    union all
     14    select 2 项目编号, 'def' 字段1, 'mno' 字段2, 321 字段3 from dual)
     15  select 项目编号, 字段1, 字段2, replace (wm_concat(字段3),',') 字段3
     16    from t
     17   group by 项目编号, 字段1, 字段2;
     
          项目编号 字段1 字段2 字段3
    ---------- ----- ----- --------------------------------------------------------------------------------
             1 abc   xyz   123488888833888
             2 def   mno   987321
     
    SQL> 
      

  4.   


    如果版本支持(9i以上)
    用vm_concat
    如果不支持
    用connect by加上sys_connect_by_path来实现了
      

  5.   

    with t as
     (select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 1234 字段3
        from dual
      union all
      select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 8888 字段3
        from dual
      union all
      select 1 项目编号, 'abc' 字段1, 'xyz' 字段2, 8833888 字段3
        from dual
      union all
      select 2 项目编号, 'def' 字段1, 'mno' 字段2, 987 字段3
        from dual
      union all
      select 2 项目编号, 'def' 字段1, 'mno' 字段2, 321 字段3 from dual)
    select  项目编号, 字段1, 字段2,max(replace(sys_connect_by_path(字段3, ','),','))
    from
    (select row_number() over(PARTITION by 项目编号, 字段1, 字段2 ORDER by 项目编号, 字段1, 字段2) r,t.*
          from t ) 
    start with r=1 
    CONNECT by prior r =r-1 
    group by 项目编号, 字段1, 字段2 ORDER by 项目编号, 字段1, 字段2;
      

  6.   

    我建议搞一个过程,很简单就可定了!
    delcare
    v_字段c  varchar2(2000);
    cursor c_cur is
    select 项目编号,字段a,字段b from table;begin
    for v_cur in c_cur 
    loop
    select v_字段c ||字段c into v_字段c from table;
    dbms_output.put_line(v_cur.项目编号||v_cur.字段a||v_cur.字段b||v_字段c);
    end loop;
    end;
      

  7.   


    还是这个比较好些,要是用2楼的查询出每一个然后在 union all 那要是有多个不就要 union all很多?那还了得,而且还是都要先查出字段3来