pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tim Barnard <tbarnard@povn.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general@postgresql.org
Subject: Re: Re: [SQL] Difficult SQL Statement
Date: Tue, 29 May 2001 09:44:35 -0700
Message-ID: <002f01c0e85e$a56b0cc0$a519af3f@hartcomm.com> (raw)
References: <f0e3dc0b.0105230920.441e873e@posting.google.com>
	<01f101c0e7c6$9fc061a0$a519af3f@hartcomm.com>
	<18343.991143963@sss.pgh.pa.us>
	<18584.991147846@sss.pgh.pa.us>

Thanks for the helpful tip!

Tim

----- Original Message ----- 
From: "Tom Lane" <tgl@sss.pgh.pa.us>
To: "Tim Barnard" <tbarnard@povn.com>
Cc: "Jim" <jim_esti@hotmail.com>; <pgsql-general@postgresql.org>
Sent: Tuesday, May 29, 2001 7:50 AM
Subject: Re: [GENERAL] Re: [SQL] Difficult SQL Statement 


> I wrote:
> > select ASMT_CODE, PASSED, TOTAL, PASSED::float4/TOTAL::float4
> > from
> >     (select ASMT_CODE, count(*) as TOTAL from RESULTS
> >      group by ASMT_CODE) as tmp1
> >   natural join
> >     (select ASMT_CODE, count(*) as PASSED from RESULTS
> >      where STATUS='PASSED' group by ASMT_CODE) as tmp2
> 
> BTW, although this is a fine example of how to avoid using temp tables,
> it's not such a great solution to the original problem.  What happens
> if there are no 'PASSED' entries at all for a given ASMT_CODE?  You
> probably won't want that ASMT_CODE to disappear from your report ---
> but it will disappear in the join.  We could fix this with a left join
> and some hackery to deal with the resulting NULL values for PASSED,
> but now things are getting ugly.  Time to try something different:
> 
> select ASMT_CODE, PASSED, TOTAL, PASSED::float4/TOTAL::float4 as PCT
> from
>     (select ASMT_CODE,
>             count(*) as TOTAL,
>             sum(CASE WHEN STATUS='PASSED' THEN 1 ELSE 0 END) as PASSED
>      from RESULTS group by ASMT_CODE) as tmp1
> 
> Here we use the sub-select only as a way of labeling the count() and
> sum() results so that we don't have to write and compute them twice.
> You could write it as a simple one-level SELECT if you didn't mind
> that redundancy.
> 
> regards, tom lane
> 




view thread (6+ messages)  latest in thread

Message-ID: <002f01c0e85e$a56b0cc0$a519af3f@hartcomm.com>
Permalink:  ../002f01c0e85e$a56b0cc0$a519af3f@hartcomm.com/
Also on:    postgresql.org/message-id/002f01c0e85e$a56b0cc0$a519af3f@hartcomm.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: tbarnard@povn.com, tgl@sss.pgh.pa.us, pgsql-general@postgresql.org
  Subject: Re: Re: [SQL] Difficult SQL Statement
  In-Reply-To: <002f01c0e85e$a56b0cc0$a519af3f@hartcomm.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