大概是这样的
HC表
行业(HY)   国家(CY)
航空    中国
航空    日本
医药    中国
it      中国
it      美国
it      日本
——————————————输出结果:
行业   国家
航空   中国,日本
医药   中国
it     中国,日本,美国求一SQL:

解决方案 »

  1.   


    --10g或以上版本
    select hy,wm_concat(cy) from hc group by hy;--如果低于10g
    --先定义字段拼接函数
    create or replace function my_concat(var_hy varchar2)
    return varchar2
    is
    v_col varchar2(4000);
    begin --将sys_mc相同的ry_mc连接起来
    for cur in (select cy from hc where hy=var_hy)
    loop
    v_col:=v_col||cur.cy||';';
    end loop;
    v_col:=rtrim(v_col,';');--去掉最后一个;
    return v_col;
    end;--select hy,my_concat(hy) from hc
      

  2.   

    --如果一定要用逗号的话
    select hy,replace(wm_concat(cy),';',',') from hc group by hy;--修改函数
    loop  v_col:=v_col||cur.cy||',';  
    end loop;  
    v_col:=rtrim(v_col,',');--去掉最后一个; 
      

  3.   

    三种方法,请参考我的博客:http://blog.csdn.net/gelyon/archive/2010/09/20/5897608.aspx
      

  4.   

    SQL> with hc as (select '航空' hy, '中国' cy from dual
      2             union
      3             select '航空' hy, '日本' cy from dual
      4             union
      5             select '医药' hy, '中国' cy from dual
      6             union
      7             select 'it' hy, '中国' cy from dual
      8             union
      9             select 'it' hy, '美国' cy from dual
     10             union
     11             select 'it' hy, '日本' cy from dual
     12             )
     13  select hy, max(substr(sys_connect_by_path(cy, ','), 2)) cy
     14    from (select rownum rn, hy, cy from hc)
     15  connect by prior rn - 1 = rn
     16         and prior hy = hy
     17   group by hy
     18  /
     
    HY   CY
    ---- --------------------------------------------------------------------------------
    it   中国,日本,美国
    医药 中国
    航空 中国,日本
     
    SQL> 10G以前的版本可以用sys_connect_by_path