在做统计中遇到以下难题
如果表T中数据如下NAME  MS张三 工人
张三 北京
李四 党员
李四 天津
李四 塘沽
.    .
.    .
.    .如何取出以下格式的数据NAME MS1
张三 工人北京
李四 党员天津塘沽请各位高人不惜赐教 感恩

解决方案 »

  1.   

    select name,replace(wm_concat(MS),',','') from t group by name
    试一下!
      

  2.   

    select name,replace(wm_concat(MS),',','') from t group by name
    10g 版本以上的应该可以
      

  3.   

    --你表里应该还有其他字段吧?决定是北京工人 还是工人北京 
    WITH tb AS(
    select '张三' name, '工人' ms from dual union all
    select '张三', '北京' from dual union all
    select '李四', '党员' from dual union all
    select '李四', '天津' from dual union all
    select '李四', '塘沽' from dual)
    --以上为提供数据的语句
    select name,replace(wm_concat(ms),',','') newms
    from tb
    group by name
    NAME NEWMS
    ---- --------------------
    李四 党员天津塘沽
    张三 工人北京
      

  4.   


    恩 有其他的 是ORA9  报 wm_concat 无效标识 什么意思?
      

  5.   

    --其他版本也可以用的
    WITH tb AS(
    select '张三' name, '工人' ms,1 flag from dual union all
    select '张三', '北京', 2 from dual union all
    select '李四', '党员', 1 from dual union all
    select '李四', '天津', 2 from dual union all
    select '李四', '塘沽', 3 from dual)
    select name,replace(substr(max(sys_connect_by_path(ms,';')),2),';','') newms
    from (select name,ms,rn,lead(rn) over(partition by name order by rn) rn1
          from (select name,ms,row_number() over(order by flag desc) rn --我加了个排序字段
                from tb)
          )
    start with rn1 is null
    connect by rn1=prior rn
    group by name
    NAME NEWMS
    ---- --------------------
    李四 党员天津塘沽
    张三 工人北京