Re: SELECT query - Mailing list pgsql-novice

From Joel Burton
Subject Re: SELECT query
Date
Msg-id Pine.LNX.4.21.0103141928340.17320-100000@olympus.scw.org
Whole thread Raw
In response to SELECT query  ("G. Anthony Reina" <reina@nsi.edu>)
List pgsql-novice
On Wed, 14 Mar 2001, G. Anthony Reina wrote:

> I think it would be something like:
> select distinct subject, arm, rep from circles_proc where rep = (select
> rep  from circles_proc where 5 = count(cycle));

You're heading towards

SELECT distinct subject, arm, rep FROM circles_proc WHERE rep IN
  (SELECT rep FROM circles_proc GROUP BY rep HAVING COUNT(rep)=5);

A possible variant would be

SELECT distinct subject, arm, rep FROM circles_proc c1 WHERE EXISTS
  (SELECT rep FROM circles_proc c2 WHERE c1.rep=c2.rep GROUP BY reg HAVING
   COUNT(rep)=5)

though w/a small set of test data, this seems less efficient.

There's also

SELECT DISTINCT subj, arm, rep FROM circles_proc c1 WHERE 5=
  (SELECT COUNT(rep) FROM circles_proc c2 where c1.rep=c2.rep);

but, again, EXPLAIN thinks this will be less efficient.


Unless you get a great solution elsewhere :-), try indexing the fields
and testing the different queries above.

Good luck,

--
Joel Burton   <jburton@scw.org>
Director of Information Systems, Support Center of Washington



pgsql-novice by date:

Previous
From: David Olbersen
Date:
Subject: Re: Which is faster, create index after many inserts or before?
Next
From: "G. Anthony Reina"
Date:
Subject: Re: SELECT query