Created
January 23, 2015 10:30
-
-
Save velll/8c778fe7e8ec8fe21cf0 to your computer and use it in GitHub Desktop.
oracle recursive subquery factoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE recursive_subquery ( | |
| id number, | |
| name varchar2(100), | |
| line_no number) | |
| insert into recursive_subquery( | |
| id, | |
| name, | |
| line_no) | |
| values( | |
| 23984, | |
| 'one', | |
| 1); | |
| insert into recursive_subquery( | |
| id, | |
| name, | |
| line_no) | |
| values( | |
| 293845, | |
| 'two', | |
| 2); | |
| insert into recursive_subquery( | |
| id, | |
| name, | |
| line_no) | |
| values( | |
| 291834, | |
| 'four', | |
| 4); | |
| insert into recursive_subquery( | |
| id, | |
| name, | |
| line_no) | |
| values( | |
| 21093, | |
| 'three', | |
| 3); | |
| select * | |
| from recursive_subquery | |
| start with line_no = 1 | |
| connect by line_no = prior line_no + 1 | |
| delete from recursive_subquery where name = 'three' | |
| select * from recursive_subquery | |
| WITH lines(id, name, line_no) AS ( | |
| SELECT id, | |
| name, | |
| line_no | |
| from recursive_subquery | |
| where line_no = 1 | |
| union all | |
| SELECT T.id, | |
| T.name, | |
| T.line_no | |
| FROM recursive_subquery T, | |
| lines L | |
| WHERE T.line_no = L.line_no + 1) | |
| select * | |
| from lines | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment