Last active
June 24, 2016 14:09
-
-
Save mrghzdf/bb4931839d0bc15461da6004e0dfb152 to your computer and use it in GitHub Desktop.
Recursive function in PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE cte_recursiva (entid,entnome,orgentidpai,seq,path, catid, orgstatus ) | |
AS | |
( | |
SELECT org.entid, ent.entnome, org.orgentidpai, 0 seq, CAST (org.entid AS varchar) as path, cat.catid, org.orgstatus | |
FROM entidade.organograma as org | |
INNER JOIN entidade.entidade as ent on ent.entid = org.entid | |
LEFT JOIN entidade.categoriaorganograma AS cat ON cat.catid = org.catid | |
WHERE org.orgentidpai = 756433 -- Filho da auditoria Interna | |
UNION ALL | |
SELECT g.entid,e.entnome, g.orgentidpai, seq+1, path || '>' || g.entid, cat.catid, g.orgstatus | |
FROM entidade.organograma as g | |
INNER JOIN entidade.entidade as e on e.entid = g.entid | |
INNER JOIN cte_recursiva as c ON c.entid = g.orgentidpai | |
INNER JOIN entidade.categoriaorganograma AS cat ON cat.catid = g.catid | |
) | |
SELECT entid FROM cte_recursiva WHERE orgstatus = 'A' ORDER BY path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Função Recursiva postgreSQL