Re: Ltree - how to sort nodes on parent node

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Harald Fuchs <hari(dot)fuchs(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Ltree - how to sort nodes on parent node
Date: 2010-04-20 18:42:33
Message-ID: 1F96E061-713C-4929-A7D9-278E5B608EE1@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 20 Apr 2010, at 18:05, Harald Fuchs wrote:

> Here's a working version:
>
> WITH RECURSIVE tree (path, category, sort_order, parent) AS (
> SELECT category, category, sort_order::text, parent
> FROM node
> WHERE parent IS NULL
> UNION ALL
> SELECT t.path || '.' || n.category,
> n.category,
> t.sort_order || '.' || n.sort_order,
> n.parent
> FROM tree t
> JOIN node n ON n.parent = t.category
> )
> SELECT path
> FROM tree
> ORDER BY sort_order

May be, but then you're just re-inventing ltree again. I'm pretty sure this must be possible without adding convoluted things like casting sort orders to text (which can for example cause issues like '10' ending up between '1' and '2').

Since this is 8.4 anyway (CTE's after all), can't the sorting be done using a windowing function or something? We have recursion now, there's got to be a proper solution, I just can't get my mind around it right now.

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4bcdf5a610412270627163!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2010-04-20 18:58:41 Re: Ltree - how to sort nodes on parent node
Previous Message Glus Xof 2010-04-20 17:53:34 Specific database vars, again...