Pre-connect with the condition

I need to write a hierarchical query with the condition Ie. If the condition is true, some columns should be used in the join according to the previous sentence, and if it is false, other columns should be indicated in the sentence.

Pseudo code ...

If col1 is not null then
  Connect by prior col1=col1
Else
  Connect by prior col2=col2 
...

Is something like this possible in plsql?

+4
source share
2 answers

You just need to properly encapsulate the conditions:

connect by (col1 is not null and prior col1=col1)
  or (col1 is null and prior col2=col2)
+1
source

Try CASE EXPRESSION:

CONNECT BY PRIOR CASE WHEN col1 IS NOT NULL THEN col1 ELSE col2 END
               = CASE WHEN col1 IS NOT NULL THEN col1 ELSE col2 END
+4
source

Source: https://habr.com/ru/post/1659426/


All Articles