pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Harald Fuchs <hari.fuchs@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: Ltree - how to sort nodes on parent node
Date: Tue, 20 Apr 2010 18:05:46 +0200
Message-ID: <pubpdecdnp.fsf@srv.protecting.net> (raw)
References: <hqh0cs$24bv$1@news.hub.org>
	<BB80D221-44CC-4174-9576-5032C0FF398F@solfertje.student.utwente.nl>
	<hqi79i$1oel$1@news.hub.org>
	<59670B22-30CB-4E6E-83C8-C1D1036C9B2A@solfertje.student.utwente.nl>

In article <59670B22-30CB-4E6E-83C8-C1D1036C9B2A@solfertje.student.utwente.nl>,
Alban Hertroys <dalroi@solfertje.student.utwente.nl> writes:

> 2). Drop the ltree column and go with a truly recursive approach, something like this:

> CREATE TABLE node (
> 	category	text	NOT NULL PRIMARY KEY,
> 	sort_order	int	NOT NULL,
> 	parent		text	REFERENCES tree (category)
> 					ON UPDATE CASCADE
> 					ON DELETE CASCADE
> );

> WITH RECURSIVE tree AS (
> 	SELECT *
> 	  FROM node
> 	 WHERE parent IS NULL

> 	UNION ALL

> 	SELECT node.*
> 	  FROM tree, node
> 	 WHERE node.parent = tree.category
> 	 ORDER BY sort_order
> )
> SELECT * FROM tree;

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




view thread (11+ messages)  latest in thread

Message-ID: <pubpdecdnp.fsf@srv.protecting.net>
Permalink:  ../pubpdecdnp.fsf@srv.protecting.net/
Also on:    postgresql.org/message-id/pubpdecdnp.fsf@srv.protecting.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hari.fuchs@gmail.com
  Subject: Re: Ltree - how to sort nodes on parent node
  In-Reply-To: <pubpdecdnp.fsf@srv.protecting.net>
  References: <hqh0cs$24bv$1@news.hub.org> <BB80D221-44CC-4174-9576-5032C0FF398F@solfertje.student.utwente.nl> <hqi79i$1oel$1@news.hub.org> <59670B22-30CB-4E6E-83C8-C1D1036C9B2A@solfertje.student.utwente.nl> <pubpdecdnp.fsf@srv.protecting.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox