pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Camila Rocha <camilarrocha@hotmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: rows in order
Date: Fri, 04 Oct 2002 08:51:42 -0700
Message-ID: <3D9DB90E.3010101@joeconway.com> (raw)
References: <OE40vN3qljWwY0VnkB4000054b0@hotmail.com>

Camila Rocha wrote:
> Is there a way to put in order the rows in a table? the problem is that i w=
> ant to keep a "tree" in the db, but the leaves must be ordered...
> does someone have an idea?

If you don't mind trying 7.3 beta, there is a function called connectby() in 
contrib/tablefunc. It works like this:

CREATE TABLE connectby_tree(keyid text, parent_keyid text);
INSERT INTO connectby_tree VALUES('row1',NULL);
INSERT INTO connectby_tree VALUES('row2','row1');
INSERT INTO connectby_tree VALUES('row3','row1');
INSERT INTO connectby_tree VALUES('row4','row2');
INSERT INTO connectby_tree VALUES('row5','row2');
INSERT INTO connectby_tree VALUES('row6','row4');
INSERT INTO connectby_tree VALUES('row7','row3');
INSERT INTO connectby_tree VALUES('row8','row6');
INSERT INTO connectby_tree VALUES('row9','row5');

SELECT * FROM
  connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~')
AS t(keyid text, parent_keyid text, level int, branch text);

  keyid | parent_keyid | level |       branch
-------+--------------+-------+---------------------
  row2  |              |     0 | row2
  row4  | row2         |     1 | row2~row4
  row6  | row4         |     2 | row2~row4~row6
  row8  | row6         |     3 | row2~row4~row6~row8
  row5  | row2         |     1 | row2~row5
  row9  | row5         |     2 | row2~row5~row9
(6 rows)

This allows completely dynamically generated trees.

There is also a contrib/ltree, which I believe creates a persistent structure 
for the tree information, and gives you tools to manipulate it (but I have 
never used it, so my discription may not be completely accurate).

HTH,

Joe




view thread (5+ messages)

Message-ID: <3D9DB90E.3010101@joeconway.com>
Permalink:  ../3D9DB90E.3010101@joeconway.com/
Also on:    postgresql.org/message-id/3D9DB90E.3010101@joeconway.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, camilarrocha@hotmail.com
  Subject: Re: rows in order
  In-Reply-To: <3D9DB90E.3010101@joeconway.com>

* 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