I have query like this
select unique so.object_id, bpi_id
from order_items,
nc_objects so
where so.object_type_id = 9062352550013045460 /* Sales Order */
start with so.object_id = order_items.object_id
connect by prior so.parent_id = so.object_id
Oracle version: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
'explain plan' shows that there is no join conditions between tables so and order_items
Can someone explain me why they are not connected by the start with
statement?
I rewrited my initial query:
select unique (select so.object_id
from nc_objects so
where so.object_type_id = 9062352550013045460 /* Sales Order */
start with so.object_id = order_items.object_id
connect by prior so.parent_id = so.object_id) so_id,
bpi_id
from order_items
It works, but only because of so
have one row per order_items
row.
If you do:
Then for the sample data:
The output is:
You can see from the path that the
so.object_id
only equals theorder_items.object_id
for theSTART WITH
row but for all the descendant rows everyso.object_id
is connected to everyorder_items.object_id
with aCROSS JOIN
.If you want to rewrite it as a recursive query then:
Which outputs the same.
You can see that the join condition
WHERE so_id = oi_id
is only applied on the first join of the recursive query, the equivalent of theSTART WITH
clause of the hierarchical query, and is not applied to any of the rows generated in the recursive part of the query, the equivalent of theCONNECT BY
clause; therefore, it is not appropriate to join the two tables using theSTART BY
clause as that only applies to the first row of the hierarchy and the rest areCROSS JOIN
ed.If you did want to
JOIN
at each level of the hierarchy then don't use aCROSS JOIN
and useINNER JOIN
instead:Which outputs:
fiddle