Please I have a table structured this way.
Number | Parent |
---|---|
1 | NULL |
2 | 1 |
3 | 2 |
I want to carry out a SQL Query that goes through the rows and for each row outputs one of two values for the case:
- If the Number is a parent of another number, the case will output "Parent" Label for that row. e.g number 1 and 2
- If the Number is not a parent i.e a leaf, the case statement will output "Not Parent" for that row. e.g Number 3.
Please, how can I do this? I really am stumped about how to structure the case statement. New to SQL.
Thanks
3
Answers
parent
are in theparent
column.leaf
are notparent
Start by selecting the start values (
SELECT ... WHERE mytable.Number=3
)In the recusrive part select the parent (
SELECT FROM cte INNER JOIN ON ....
)see: DBFIDDLE
You can also use EXISTS