oracle 聚合 分散 SQL
来源:互联网 发布:数据链路层mac功能 编辑:程序博客网 时间:2024/04/28 13:00
SELECT l_count, REGEXP_SUBSTR('add, daddf, dsdf, asdfa, dsfasd, dsfad','[^,]+',1,l_count) AS NAME
FROM dual
,(SELECT LEVEL l_count FROM DUAL CONNECT BY LEVEL<=100)
WHERE l_count <=LENGTH('add, daddf, dsdf, asdfa, dsfasd, dsfad') - LENGTH(REPLACE('add, daddf, dsdf, asdfa, dsfasd, dsfad',','))+1
select replace(regexp_substr(:idstring,'[^,]+',1,level),',',' ')
from dualconnect by level<=length(:idstring)-length(replace(:idstring,',',''))+1
一条SQL的法则是某位大师提出的,大家曾经在某个时期都乐此不彼的寻求各种的问题一条SQL法,但是大师的意思似乎被曲解,很多性能差,可读性差,灵活差的SQL都是这个原则产物,所谓画虎不成反成犬类。不过,解决问题始终是第一原则,这里还是给出一个比较有代表性的一条SQL方法。
SELECT country,max(substr(city,2)) city
FROM
(SELECT country,sys_connect_by_path(city,',') city
FROM
(SELECT country,city,country||rn rchild,country||(rn-1) rfather
FROM
(SELECT test.country ,test.city,row_number() over (PARTITION BY test.country ORDER BY
test.city) rn
FROM test))
CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0')
GROUP BY country;
下面分步解析,有4个FROM,就有4次结果集的操作。
step 1 给记录加上序号rn
SQL> SELECT test.country ,test.city,row_number() over (PARTITION BY test.country ORDER
BY test.city) rn
2 FROM test
3 /
COUNTRY CITY RN
-------------------- -------------------- ----------
日本 大阪 1
日本 东京 2
中国 上海 1
中国 台北 2
中国 香港 3
step 2 创造子节点父节点
SQL> SELECT country,city,country||rn rchild,country||(rn-1) rfather
2 FROM
3 (SELECT test.country ,test.city,row_number() over (PARTITION BY test.country ORDER
BY test.city) rn
4 FROM test)
5 /
日本 大阪 日本1 日本0
日本 东京 日本2 日本1
中国 上海 中国1 中国0
中国 台北 中国2 中国1
中国 香港 中国3 中国2
step 3 利用sys_connect_by_path生成结果集
SELECT country,sys_connect_by_path(city,',') city
FROM
(SELECT country,city,country||rn rchild,country||(rn-1) rfather
FROM
(SELECT test.country ,test.city,row_number() over (PARTITION BY test.country ORDER BY
test.city) rn
FROM test))
CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0'
日本 ,大阪
日本 ,大阪,东京
中国 ,上海
中国 ,上海,台北
中国 ,上海,台北,香港
step 4 最终步骤,筛选结果集合
SQL> SELECT country,max(substr(city,2)) city
2 FROM
3 (SELECT country,sys_connect_by_path(city,',') city
4 FROM
5 (SELECT country,city,country||rn rchild,country||(rn-1) rfather
6 FROM
7 (SELECT test.country ,test.city,row_number() over (PARTITION BY test.country ORDER
BY test.city) rn
8 FROM test))
9 CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0')
10 GROUP BY country;
COUNTRY CITY
-------------------- -------
中国 上海,台北,香港
日本 大阪,东京
可谓是,7歪8搞,最后还是弄出来了,呵呵。
- oracle 聚合 分散 SQL
- ORACLE SQL 聚合(多行)聚合函数
- SQL函数-oracle常用聚合函数
- SQL函数-Oracle 聚合函数详解
- 37.Oracle数据库SQL开发之 使用聚合函数
- Oracle RAC LoadBalance 之 Service分散负载(业务分散负载)
- Oracle分析聚合函数
- oracle自定义聚合函数
- oracle 聚合函数
- Oracle中的聚合函数
- Oracle中的聚合函数
- Oracle 字符串聚合函数
- oracle字符串聚合
- oracle聚合函数
- Oracle 聚合字符串
- oracle聚合函数全记录
- oracle聚合与分组
- oracle聚合函数
- 查询工程中的图片是否被引用 对于后期优化非常有用
- 文件管理
- 程序学习过程中存在的误区
- 进程与线程的区别
- 创建iis网站后 局域网不能访问
- oracle 聚合 分散 SQL
- 过去两月运行Cydia设备超2000万
- xxxx码
- 对C++中返回对局部变量引用的理解
- White List
- hadoop单表关联
- android gridview中处理对象的回收
- 21-编写iOS应用程序
- 有用的小代码