You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WITH RECURSIVE rec AS (
SELECT id, item_id, parent_id, 1AS level FROM _test
WHERE parent_id =1-- STARTUNION ALLSELECTc.id, c.item_id, c.parent_id, (p.level+1) FROM _test c JOIN rec p ONc.parent_id=p.item_id
) SELECT DISTINCT item_id, level FROM rec ORDER BY level ASC;
get parents recursively
WITH RECURSIVE rec AS (
SELECT id, item_id, parent_id, 1AS level FROM _test
WHERE item_id =2-- STARTUNION ALLSELECTc.id, c.item_id, c.parent_id, (p.level+1) FROM _test c JOIN rec p ONc.item_id=p.parent_id
) SELECT DISTINCT parent_id, level FROM rec WHERE parent_id IS NOT NULLORDER BY level ASC;
WITH RECURSIVE rec AS (
SELECTd.id, l.father_id, l.mother_id, 1AS level
FROM humans as d JOIN humans_births as ld ONld.human_id=d.idJOIN births as l ONl.id=ld.birth_idWHEREd.id=1-- START UNION ALLSELECTd.id, l.father_id, l.mother_id, (p.level+1)
FROM humans as d JOIN humans_births as ld ONld.human_id=d.idJOIN births as l ONl.id=ld.birth_idJOIN rec p ON (d.id=p.father_idORd.id=p.mother_id) WHEREp.level+1<=5
) SELECT father_id, mother_id FROM rec;
WITH RECURSIVE t(child, parentlist) AS (
SELECT child , ARRAY[]::INTEGER[] FROM rel WHERE parent IS NULLUNIONSELECTrel.child, rel.parent||t.parentlistFROM rel
JOIN t ONrel.parent=t.child
) SELECT*FROM t;