oracle 同列非数字字符串的拼接

来源:互联网 发布:模拟手柄软件 编辑:程序博客网 时间:2024/05/01 16:33

oracle 不支持 wm_concat 的解决办法


sys_region(id,name)

1    a

1    b

2    c

2    d

想要得到结果

1  a,b

2  c,d


这种需求其实可以用 wm_concat 这个方法来实现

也可以用自己写的function

要是自己当前的数据库不支持 wm_concat 又不想自己写function  那么可以考虑用下面的方法来实现:

select id,substr(max(sys_connect_by_path(name,',')),2) name
  from (select a.*,row_number()over(partition by id order by name) rn from sys_region a )
  group by id
start with rn=1
  connect by rn-1=prior rn and id=prior id
    order by 1
原创粉丝点击