Oracle 学习笔记(14)

非常感谢ITEYE的czjxdm

关于多行记录合并的做法:

contry_id city_id ext1 ext2

0081 101 东京 日本 
0081 102 大阪 日本 
0086 601 北京 中国 
0086 602 上海 中国 
0086 603 香港 中国 

感兴趣的方法1:

SELECT ext2,max(substr(ext1,2)) ext1
FROM
  (SELECT ext2,sys_connect_by_path(ext1,',') ext1
   FROM
     (SELECT ext2,ext1,ext2||rn rchild,ext2||(rn-1) rfather     
      FROM
        (SELECT  city_mst.ext2 ,city_mst.ext1,row_number() over (PARTITION BY city_mst.ext2 ORDER BY city_mst.ext1) rn
         FROM city_mst))
CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0') 
GROUP BY ext2;

因为他使用到了sys_connect_by_path,很不错的一个启发,原来SQL也可以有想法的,当然哥也写过类似的东西,就是把java程序转换成SQL来做

结果就是性能很低~~,不过他的这个性能也不高。sys_connect_by_path还是要继续研究呀。

方法2:

select ext2,listagg(ext1,',') within group (order by 1) from city_mst group by ext2;

方法3:
select ext2, wm_concat(ext1) from city_mst group by ext2;

方法4:有点鸡肋了,但是还是想借鉴下别人的想法

select
country_id,
max(case when city_id = '101' then ext1||',' else '' end) ||
max(case when city_id = '102' then ext1||',' else '' end) ||
max(case when city_id = '601' then ext1||',' else '' end) ||
max(case when city_id = '602' then ext1||',' else '' end) ||
max(case when city_id = '603' then ext1||',' else '' end) as citys--*/
from city_mst group by country_id

果然Oracle好强大~~

posted on 2012-12-11 20:50  扭头撞到墙  阅读(174)  评论(0编辑  收藏  举报