Getting rows in a very specific order

From: Gordon <gordon(dot)mcvey(at)ntlworld(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Getting rows in a very specific order
Date: 2008-09-08 16:29:44
Message-ID: c46d0045-cb69-4f55-8710-e39190995b84@t54g2000hsg.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm considering using an array of ints column in a table which lists a
row's ancestry. For example, if item 97 is contained within itme 68
and that item is contained with in 31 and that item is contained
within item 1 then the value of the hierachy column would be
{1,31,68,97}, the numbers refer to the primary keys of the rows.

If I use the value of the hierarchy column in a query I can get all
the rows that a given row is an descendant of. (SELECT * FROM items
WHERE itm_id IN (1,31,68,97), for example. However, I need the rows
to be in the correct order, ie the root node first, child second,
grandchild third etc. I'm guessing the order can be set with an ORDER
BY, but I've only ever used ORDER BY to order by ascending or
descending order of a single column. Is there a way to retrieve the
rows explicitly in the order they are listed in the hierarchy array?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2008-09-08 16:36:48 Re: recover in single-user backend fails
Previous Message Alejandro D. Burne 2008-09-08 16:04:01 Re: [GENERAL] secure connections