Re: Re: [SQL] Difficult SQL Statement

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Tim Barnard" <tbarnard(at)povn(dot)com>
Cc: "Jim" <jim_esti(at)hotmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Re: [SQL] Difficult SQL Statement
Date: 2001-05-29 13:46:03
Message-ID: 18343.991143963@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

"Tim Barnard" <tbarnard(at)povn(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dave Robinson 2001-05-29 13:55:19 Re: transactions on Postgresql
Previous Message Tod McQuillin 2001-05-29 13:32:30 Re: 7.1.2

Browse pgsql-sql by date

  From Date Subject
Next Message george young 2001-05-29 14:27:51 Re: sql user management
Previous Message Anatoly K. Lasareff 2001-05-29 06:29:07 Re: Select for LEFT JOIN