Oracle 学习笔记(14)
2012-12-11 20:50
232 查看
非常感谢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好强大~~
关于多行记录合并的做法:
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好强大~~
相关文章推荐
- Oracle 学习笔记 14 -- 集合操作和高级子查询
- Oracle 学习笔记 14 -- 集合操作和高级子查询
- Oracle 备份与恢复学习笔记(14)
- Oracle 备份与恢复学习笔记(14)
- Oracle 学习笔记14 —— 函数
- oracle优化学习笔记
- oracle 学习笔记
- oracle的学习笔记
- oracle归档模式学习笔记(二)
- Oracle 9i/10g/11g编程艺术(2e)学习笔记【第2章】
- Oracle 11g 学习笔记 (3)
- Oracle专家高级编程学习笔记(一)
- oracle 学习笔记
- oracle 学习笔记 - 一次插入多条数据
- Oracle 11g 学习笔记 (4)
- oracle 体系结构 学习笔记
- MatLab建模学习笔记14——K-Means聚类算法
- oracle 学习笔记 Rman脚本(windowns)
- Oracle PL/SQL语句基础学习笔记(下)
- iOS学习笔记14-网络(三)WebView