递归查询部门路径信息:
WITH RECURSIVE T (ID, NAME, PARENT_ID, PATH, DEPTH) AS (
SELECT ID, NAME, PARENT_ID, id::text AS PATH, 1 AS DEPTH
FROM org_department
WHERE PARENT_ID =0
UNION ALL
SELECT D.ID, D.NAME, D.PARENT_ID, concat(T.PATH,'.' ,D.ID::text), T.DEPTH + 1 AS DEPTH
FROM org_department D
JOIN T ON D.PARENT_ID = T.ID
) SELECT id,PATH FROM T
parentId为部门的父级id,path为路径信息,以“.”分隔