Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. select id, 'Leaf' as node
  2. from tree
  3. where id NOT IN ( select distinct t1.id as node
  4. from tree t1 join tree t2 on t1.id = t2.p_id )
  5. union
  6. select distinct p_id, 'Inner' as node
  7. from tree
  8. where p_id NOT IN( select id from tree where p_id IS NULL)
  9. union
  10. select id, 'Root'
  11. from tree
  12. where p_id is NULL
  13. order by id
  14.  
  15. id p_id
  16. -----------
  17. 1 2
  18. 3 2
  19. 6 8
  20. 9 8
  21. 2 5
  22. 8 5
  23. 5 (null)
  24.  
  25. 1 Leaf
  26. 2 Inner
  27. 3 Leaf
  28. 5 Root
  29. 6 Leaf
  30. 8 Inner
  31. 9 Leaf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement