Mike Winichenko
asked on
Please, explain strange behavior of connect by
Hello! There's a (stripped off version of real) SQL query. I understand its logic. I can't understand why it leaves only 2 records of level 1 after uncommenting AND...
Can anybody explain? (http://sqlfiddle.com/#!4/56df0e/17)
SELECT id,PRIOR id,level n
FROM(SELECT'aaa' AS id,2 AS ticket_cnt
FROM dual
UNION ALL
SELECT 'bbb' AS id,4 AS ticket_cnt
FROM dual
)
CONNECT BY NOCYCLE (
level <= ticket_cnt
--AND id = PRIOR id
)
ORDER BY 3 ,1 ,2;
And by the way, why there is cycle (comment off nocycle - you'll see)?
Can anybody explain? (http://sqlfiddle.com/#!4/56df0e/17)
SELECT id,PRIOR id,level n
FROM(SELECT'aaa' AS id,2 AS ticket_cnt
FROM dual
UNION ALL
SELECT 'bbb' AS id,4 AS ticket_cnt
FROM dual
)
CONNECT BY NOCYCLE (
level <= ticket_cnt
--AND id = PRIOR id
)
ORDER BY 3 ,1 ,2;
And by the way, why there is cycle (comment off nocycle - you'll see)?
Nothing further to add. I endorse Slightnw's comments.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
The PRIOR you have creates a loop in the data. If you comment in the AND and remove NOCYCLE, you'll get an error:
Open in new window
From the docs:
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Hierarchical-Query-Pseudocolumns.html#GUID-2F2FBA6F-2FD1-47D6-A74F-DB4B31E4D400
>> can't understand why it leaves only 2 records of level 1 after uncommenting AND...
I'm not thinking it is just level=1. It returns "if the current row has a child which is also its ancestor"
I got that phrase from the same doc link above for the CONNECT_BY_ISCYCLE Pseudocolumn .
You can see that if you add it to your query:
Open in new window
All that said, are you sure it is returning what you want? I've not been successful using a column value in a connect by level like that. My guess is the PRIOR and NOCYCLE is because without them, the query generated a LOT of unnecessary rows.