pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Rachel Coin <rachel@derniere-minute.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: Categories and subcategories : more details
Date: Tue, 20 Mar 2001 09:43:20 -0800
Message-ID: <3AB796B8.AD87AE95@agliodbs.com> (raw)
References: <00c901c0b15e$5b7dff00$6101a8c0@spidinfo.com>

Rachel,

	See below:

> Examples :
> 
> table CATEG :
> ida     categ
>  1        X
>  2        Y
>  3        Z
> 
> table SUBCATEG :
> idb     subcateg      ida
> 1           x1             1
> 2           x2             1
> 3           x3             1
> 4           x4             1
> 5           y1             2
> 6           y2             2
> 7           z1             3
> 
> I 'd like to obtain a table with maximum 2 subcateg per categ :
> 
> table result :
> 
> categ     subcateg
>   X           x1
>   X           x2
>   Y           y1
>   Y           y2
>   Z           z1
> 

	Without LIMIT in subselects, you're in performance hell as far as doing
the above is concerned.  Frankly, I can't see any way to do it that
doesn't involve a PL/pgSQL procedure that generates a temporary table,
and has to run once for every row in your result set.  I'll post code
later if nobody comes up with a better solution.

	If you knew all of your categories in advance, you could do this via an
annoying UNION statement.  I'm assuming, however, that it needs to be
dynamic.

-Josh Berkus


-- 
______AGLIO DATABASE SOLUTIONS___________________________
                                        Josh Berkus
   Complete information technology      josh@agliodbs.com
    and data management solutions       (415) 565-7293
   for law firms, small businesses       fax  621-2533
    and non-profit organizations.       San Francisco



view thread (2+ messages)

Message-ID: <3AB796B8.AD87AE95@agliodbs.com>
Permalink:  ../3AB796B8.AD87AE95@agliodbs.com/
Also on:    postgresql.org/message-id/3AB796B8.AD87AE95@agliodbs.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 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-sql@postgresql.org
  Cc: josh@agliodbs.com, rachel@derniere-minute.org
  Subject: Re: Categories and subcategories : more details
  In-Reply-To: <3AB796B8.AD87AE95@agliodbs.com>
  References: <00c901c0b15e$5b7dff00$6101a8c0@spidinfo.com> <3AB796B8.AD87AE95@agliodbs.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