pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joseph Shraibman <jks@selectacast.net>
To: Josh Berkus <josh@agliodbs.com>
Cc: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: group by weirdness
Date: Thu, 13 Sep 2001 17:53:42 -0400
Message-ID: <3BA12AE6.9050708@selectacast.net> (raw)
References: <web-116932@davinci.ethosmedia.com>



Josh Berkus wrote:

> Try putting your sub-selects in the FROM clause instead.  (Personally,
> I've never found a use for sub-selects in the SELECT clause)
> 
> SELECT j.id, j.created, count(mj.mid), ma1.mcount, ma2.mcount
> FROM j, mj,
> 	(SELECTjid, COUNT(oid) as mcount FROM ml
> 		WHERE ml.state <> 11 GROUP BY jid) ma1,
> 	(SELECT jid, COUNT(oid) as mcount FROM ml
> 		WHERE ml.state in (2,5) GROUP BY jid) ma2
> WHERE j.fkey = 1 AND mj.jid = j.id
>   AND ma1.jid = j.id AND ma2.jid = j.id
> GROUP BY j.id, j.created, ma1.mcount, ma2.mcount;
> 

OK that worked for this simple example, but on my real database the performance was 
horrible, and it didn't work for then there were zero entries in ml (this bites me 
sometimes, when the AND clause keeps things from working as I think they should).  Putting 
the selects in the SELECT solved both problems.  I took out the 'AND ml.jid = j.id' from 
the outer WHERE (would have also excluded cases where there were zero entries in ml) and 
only refrenced ml in the subselect.

Thanks for your help.





-- 
Joseph Shraibman
jks@selectacast.net
Increase signal to noise ratio.  http://www.targabot.com




view thread (7+ messages)  latest in thread

Message-ID: <3BA12AE6.9050708@selectacast.net>
Permalink:  ../3BA12AE6.9050708@selectacast.net/
Also on:    postgresql.org/message-id/3BA12AE6.9050708@selectacast.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jks@selectacast.net, josh@agliodbs.com
  Subject: Re: group by weirdness
  In-Reply-To: <3BA12AE6.9050708@selectacast.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