Re: WITH RECURSIVE clause -- all full and partial paths

From: Harald Fuchs <hari(dot)fuchs(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: WITH RECURSIVE clause -- all full and partial paths
Date: 2009-06-12 20:14:21
Message-ID: puzlcd2orm.fsf@srv.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In article <aaf543e90906120856r5219cf9cv7f13ba0d37494378(at)mail(dot)gmail(dot)com>,
aryoo <howaryoo(at)gmail(dot)com> writes:

> Dear list,
> In reference to the message below posted on the 'pgsql-hackers' list regarding
> 'iterative' queries,
> could anyone help me write the queries that return all full and all partial
> paths from the root?

Probably you want to use the following query:

WITH RECURSIVE subdepartment AS (
SELECT id, parent_department, name AS path
FROM department
WHERE name = 'A'
UNION ALL
SELECT d.id, d.parent_department, sd.path || '.' || d.name
FROM department d
JOIN subdepartment sd ON sd.id = d.parent_department
)
SELECT id, path
FROM subdepartment;

This returns:

id | path
----+---------
1 | A
2 | A.B
3 | A.B.C
4 | A.B.D
6 | A.B.C.F
(5 rows)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2009-06-12 20:25:17 Re: search for partial dates
Previous Message Christine Penner 2009-06-12 19:36:27 String Manipulation