`

connect by sql

 
阅读更多
用法实例:

Basically if table has one row, we can get what we expected.
If more than one rows, totally mass and we need to use distinct to get what we want.
But if the table has many rows, it will be very slow.
Below is the test sql for your references:
create table test(id int, a int, b int);
insert into test values(1,5,8);

-- we are expecting 4 rows of 5,6,7,8, below sql can do
select id,a,b, a+level-1 from test connect by level<=b-a+1;

-- add one more row to the table
insert into test values(2,10,13);

-- we are expecting 4 rows of 5,6,7,8 and 4 rows of 10,11,12,13, so totally 8
-- below sql works but without distinct got 30 rows
select distinct id,a,b, a+level-1 c from test connect by level<=b-a+1 order by a, c;

-- if we add many rows to table, above sql will run forever, i think due to too much connect by and distinct
insert into test select rownum+2,rownum, rownum+3 from dual connect by level<=1000;
select distinct id,a,b, a+level-1 c from test connect by level<=b-a+1 order by a, c; -- run forever

-- here comes a interesting sql i noticed from internet, it works well and also very quick
-- any idea about it, i can not understand
-- i guess the key point is what does CONNECT BY exactly do in ORACLE
select id,a,b,a+level-1 c from test connect by level<=b-a+1 and prior id=id and prior dbms_random.value is not null order by a,c;

表结构:
pm_ci ci_id stu_ids
       1     1,2,3,4
       2     1,4
查询结果为:
        1   1
        1   2
        1   3
        1   4
         2   1
         2    4
其中需要注意正则表达式的用法
SELECT ci_id, regexp_substr(stu_ids, '[^,]+', 1, level) stu_id, level
  FROM pm_ci
CONNECT BY level <= length(regexp_replace(stu_ids, '[^,]+')) + 1
  and PRIOR ci_id = ci_id
         AND PRIOR dbms_random.value is not null
order by ci_id asc, stu_id asc

oracle的行列转换函数:wm_concat
select t.pay_orderno,
       wm_concat(t.agent_orderno) ordernolist,
       count(*) countnum
  from eir_pay t
where t.pay_orderno is not null
group by pay_orderno
having count(*) > 2
查询一个订单号对应的多个业务号

另外oracle的分析函数 lag和lead 也可实现某些特殊的需求
分享到:
评论

相关推荐

    start connect by

    sql语句中的经常用到start connect by,这里简单介绍一下它的使用

    Oracle sql 学习笔记1 利用rollup和connect by实现报表统计

    在日常收入报表统计中,通常用到机构代码、行业代码、征收项目代码等树形结构表单。比如:分单位分级次统计收入完成情况、分sz情况统计表、分单位分sz统计收入完成情况、分行业统计收入完成情况等。...

    浅谈Oracle下connect by原理.pdf

    Oracle Connect by

    SQL21日自学通

    TNS:listener Could Not Resolve SID Given in Connect Descriptor 484 Insufficient Privileges During Grants484 Escape Character in Your Statement--Invalid Character 485 Cannot Create Operating System ...

    hivesql语句练习

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; 6. Jline包版本不一致的问题,需要拷贝hive的lib目录中jline.2.12.jar的jar包替换掉hadoop中的 /home/...

    程序员的SQL金典.rar

     11.9.1 Oracle中的CONNECT BY子句  11.9.2 Oracle中的SYS_CONNECT_BY_PATH()函数  11.9.3 My SQL Server和DB2中递归查询 附录A 常用数据库系统的安装和使用  A.1 DB2的安装和使用  A.2 MySQL的安装和使用  A....

    SQL培训第一期

    connect by prior t.parentcode = t.organcode; //递归查询父节点 select t.* from g_organ t start with t.organcode = '080' connect by t.parentcode = prior t.organcode; //递归查询子节点 1.8.7 union 和 ...

    查询重复数据sql语句

    是一个简单的sql语句,你可以仿照这个语句查出表里的重复数据。

    sql最全的常用命令语句

    CROSS APPLY sys.dm_exec_sql_text( s.sql_handle ) t ORDER BY s.max_physical_reads DESC SELECT SUM(signal_wait_time_ms) AS total_signal_wait_time_ms总信号等待时间 , SUM(wait_time_ms - signal_...

    程序员的SQL金典4-8

     11.9.1 Oracle中的CONNECT BY子句  11.9.2 Oracle中的SYS_CONNECT_BY_PATH()函数  11.9.3 My SQL Server和DB2中递归查询 附录A 常用数据库系统的安装和使用  A.1 DB2的安装和使用  A.2 MySQL的安装和使用  A....

    sql查询某个parentid下的所有childid

    sql查询某个parentid下的所有childid

    MySQL ConnectorPython Revealed SQL and NoSQL Data Storage

    By the end of the book, you will be able to use MySQL as the back-end storage for your Python programs, and you’ll even have the option of choosing between SQL and NoSQL interfaces. What You’ll ...

    A Step By Step Guide To Learning SQL

    SQL 是用于访问和处理数据库的标准的计算机语言。 一步一步学习sql语言&gt;&gt;,英文书籍,pdf版本

    SQL性能优化

    CONNECT BY PRIOR ID = parent_id AND STATEMENT_ID = user_define  示例 如要测试下面SQL: SELECT c.short, a.cday, a.card_no, a.qty FROM sales.stockiohis a, sales.product_info b, sales.vendor c WHERE ...

    Oracle sqldeveloper without jdk (win+linux)

    - The script to which the character belongs is supported by the JRE installation on which SQL Developer is running � for example, appropriate fonts are available � and - The script does not ...

    使用BULK COLLECT, MERGE 语句提高sql执行效率

    详细介绍了使用 BULK COLLECT 进行批量操作 提高sql的执行效率 使用MERGE INTO USING 一条sql搞定 新增和修改 使用connect by 进行递归树查询

    oracle sql 行列转换

    行列转换,sys_connect_by_path,row_number等函数的用法

    SQL袖珍参考手册(第3版)

    CONNECT BY Queries Data Type Conversion Data Types: Binary Integer Data Types: Character String Data Types: Datetime Data Types: Decimal Datetime Conversions: DB2 Datetime Conversions: MySQL Datetime ...

    Db2_SQL命令大全(完整版)

    DB2/SQL命令大全 连接数据库:  connect to [数据库名] user [操作用户名] using [密码] 创建缓冲池(8K):  create bufferpool ibmdefault8k IMMEDIATE SIZE 5000 PAGESIZE 8 K ; 创建缓冲池(16K)(OA_...

Global site tag (gtag.js) - Google Analytics