Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 402C46325C9 for ; Fri, 10 Jul 2009 09:04:44 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 53569-04 for ; Fri, 10 Jul 2009 09:04:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from j83.attiksystem.ch (j83.attiksystem.ch [83.222.130.83]) by mail.postgresql.org (Postfix) with ESMTP id 90FC363246B for ; Fri, 10 Jul 2009 09:04:33 -0300 (ADT) Received: from cust.static.213-200-238-69.cybernet.ch ([213.200.238.69] helo=email.attiksystem.ch) by j83.attiksystem.ch with esmtp (Exim 4.63) (envelope-from ) id 1MPEq6-00086q-Tl for pgsql-sql@postgresql.org; Fri, 10 Jul 2009 14:04:30 +0200 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Subject: Re: WITH RECURSION output ordering with trees X-MimeOLE: Produced By Microsoft Exchange V6.0.6619.12 Date: Fri, 10 Jul 2009 13:50:22 +0200 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] WITH RECURSION output ordering with trees Thread-index: AcoBTvHOBtETEuGQSuezQPvgotCjVQAAvlyw References: From: "Philippe Lang" To: "Thomas Kellerer" , X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/58 X-Sequence-Number: 32799 pgsql-sql-owner@postgresql.org wrote: > Philippe Lang, 10.07.2009 11:10: >> Hi, >>=20 >> I'm playing with the new "WITH RECURSIVE" feature of 8.4. I'm trying >> to figure out how to use it with trees. >>=20 >> Here is the test code I use: >>=20 >> I'd like to perform a real recursion, and show the tree structure in >> a more appopriate way, like this: >>=20 >> Any idea how to do that? (without trying to sort on the lookup >> column, whose values can be random outside this test) >=20 >=20 > The manual has a nice hint on this adding up IDs to "generate" a path > like column that can be used for sorting.=20 >=20 > Try the following: >=20 > WITH RECURSIVE parse_tree (depth, id, lookup, parent_id, sort_path) > AS ( SELECT 0, > parent.id, > cast(parent.lookup as text), > parent.parent_id, > array[0] as sort_path > FROM recursion_sample parent > WHERE parent_id IS NULL > UNION ALL > SELECT > parent.depth + 1, > child.id, > rpad(' ', depth * 2) || child.lookup, > child.parent_id, > parent.sort_path || child.id > FROM parse_tree parent JOIN recursion_sample child on > child.parent_id =3D parent.id ) > select id, lookup > from parse_tree > order by sort_path > ; >=20 > This will output: >=20 > id | lookup > -----+-------- > 1 | a1 > 2 | b11 > 243 | c113 > 645 | c111 > 823 | c112 > 6 | b12 > 583 | c122 > 845 | c121 > 9 | b13 > 10 | c131 > (10 rows) Hi Thomas, Thanks for your answer. Si there a built-in function that would allow generating the sort path based on the value of the lookup column, instead of the id, which has no meaning at all? If yes, we would get instead: depth | id | lookup | parent_id -------+-----+--------+----------- 0 | 1 | a1 | 1 | 2 | b11 | 1 2 | 645 | c111 | 2 2 | 823 | c112 | 2 2 | 243 | c113 | 2 1 | 6 | b12 | 1 2 | 845 | c121 | 6 2 | 583 | c122 | 6 1 | 9 | b13 | 1 2 | 10 | c131 | 9 Best regards, Philippe Lang