postgresql - What is the proper syntax for aliasing a recursive CTE in SQL? -
i have postgresql database of tasks , subtasks. here recursive cte statement:
with recursive getchildren (childname, childid, parentid, parentname) ( select child.name,child.id,child.parent,parent.name lists child, lists parent child.parent=parent.id , parent.id=30 union select l.name,l.id,l.parent,g.childname lists l, getchildren g l.parent=g.childid ) select childid,childname,parentid,parentname getchildren;
here output:
childid | childname | parentid | parentname ---------+-----------------------------+-------------+----------------- 46 | fix escaped chars | 30 | list program 51 | add comments | 30 | list program 47 | rename task | 30 | list program 36 | dependencies | 30 | list program 34 | obfuscate id | 30 | list program 37 | move task | 36 | dependencies 82 | second order | 36 | dependencies 38 | reorder | 36 | dependencies 83 | third order | 82 | second order
i want integrate operation statement removes results parents: in other words, display 'leaves' of tree.
if output of latter query table, or if spooled results temporary table called 'temptab', want run query this:
select childname temptab childname not in (select parentname temptab);
to achieve results this:
childid | childname | parentid | parentname ---------+-----------------------------+-------------+----------------- 46 | fix escaped chars | 30 | list program 51 | add comments | 30 | list program 47 | rename task | 30 | list program 34 | obfuscate id | 30 | list program 37 | move task | 36 | dependencies 38 | reorder | 36 | dependencies 83 | third order | 82 | second order
can alias first recursive cte query , combine these 2 queries one?
le voila...
with recursive getchildren(childname,childid,parentid,parentname) ( select child.name,child.id,child.parent,parent.name lists child, lists parent child.parent=parent.id , parent.id=39 union select l.name,l.id,l.parent,g.childname lists l, getchildren g l.parent=g.childid), nonatomic (select parentname getchildren) select childname getchildren childname not in (select * nonatomic);
...using multiple withs.
Comments
Post a Comment