Re: Re: [SQL] Difficult SQL Statement - Mailing list pgsql-general

From Tom Lane
Subject Re: Re: [SQL] Difficult SQL Statement
Date
Msg-id 18343.991143963@sss.pgh.pa.us
Whole thread Raw
In response to Re: [SQL] Difficult SQL Statement  ("Tim Barnard" <tbarnard@povn.com>)
Responses Re: Re: [SQL] Difficult SQL Statement  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
"Tim Barnard" <tbarnard@povn.com> writes:
> To my thinking there's got to be a better way to do this whithout so many
> temporary tables.

In 7.1 you can frequently replace temp tables with subselect-in-FROM.
Cutting-and-pasting freely from your solution:

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

(haven't tried this, but it looks right...)

This won't necessarily be a whole lot faster than the solution with
temp tables, but it's nice not to have to worry about dropping the
temp tables afterwards.

            regards, tom lane

pgsql-general by date:

Previous
From: Thierry Besancon
Date:
Subject: Re: 7.1.2
Next
From: "Ian Harding"
Date:
Subject: Re: UPDATE keyword