表数据如下:、
上海市        门店一
上海市        门店二
上海市        门店三
上海市        门店四
上海市        门店五
北京市        门店一
北京市        门店二查询结果:
上海市:门店一、门店二、门店三、门店四、门店五;北京市:门店一、门店二需要无限制级的。因为城市列表可能添加或者减少。谢谢!!

解决方案 »

  1.   

    select col1,wm_concat(col2)
    from tablename
    group by col1
      

  2.   

    with tmp as (
    select '上海市' as city,'门店一' as store from dual union all
    select '上海市' as city,'门店二' as store from dual union all
    select '上海市' as city,'门店三' as store from dual union all
    select '上海市' as city,'门店四' as store from dual union all
    select '北京市' as city,'门店一' as store from dual union all
    select '北京市' as city,'门店二' as store from dual
    )
    select city,wm_concat(store) from tmp group by city
      

  3.   

    这2个大哥的应该都是oracle 10或 以后的写法 oracle10g之前没有 wm_concat 这个函数吧 ,这时就需要自己写一个函数 来在查询中使用 如需要再回帖,呵呵
      

  4.   

    用基本语法帮你写了一下。SELECT A, SYS_CONNECT_BY_PATH (B, ',') AS B
          FROM (SELECT A, B,RANK () OVER (ORDER BY A) 
                       + ROW_NUMBER () OVER (ORDER BY A) rn, 
                       ROW_NUMBER () OVER (PARTITION BY A ORDER BY A) rm 
                  FROM testab) a1 
         WHERE a1.ROWID IN (SELECT MAX (a2.ROWID) FROM testab a2 WHERE a2.A = a1.A) 
    START WITH rm = 1 
    CONNECT BY PRIOR rn = rn - 1 
    testab测试表,建表,如下:
    create table TESTAB
    (
      A VARCHAR2(64),
      B VARCHAR2(64)
    )
    输出结果如下:
    北京  ,门店一
    上海  ,门店二,门店一
      

  5.   


    需要需要。。
    大哥这个回复不错。。
    我用的是oracle 11g