Re: returning count(*) when it is > 1, else -1

From: Gerardo Herzig <gherzig(at)fmed(dot)uba(dot)ar>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Richard Huxton <dev(at)archonet(dot)com>, postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: returning count(*) when it is > 1, else -1
Date: 2008-10-17 12:50:26
Message-ID: 48F88A12.3000900@fmed.uba.ar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Tom Lane wrote:
> Richard Huxton <dev(at)archonet(dot)com> writes:
>> SELECT
>> CASE WHEN total >0 THEN total ELSE -1 END AS new_total
>> FROM (
>> SELECT count(*) AS total FROM test WHERE id=$1
>> ) AS raw_total
>
> Actually you could just do
>
> SELECT
> CASE WHEN count(*) >0 THEN count(*) ELSE -1 END AS total
> FROM test WHERE id=$1;
>
> PG has avoided redundant calculations of duplicate aggregates for some
> time. (This doesn't help in the original formulation because it
> actually had two different sub-selects; the case that is handled is
> identical aggregate expressions within SELECT list or HAVING of a single
> SELECT.)
>
> regards, tom lane
>
Thanks Tom! I like this one! It seems more readable to me.

Thank you all! Yeah, even you Achilleas bastard (no ofense) :)
Gerardo

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message aldy 2008-10-20 08:23:59 my rules wont execute
Previous Message Tom Lane 2008-10-17 12:32:32 Re: returning count(*) when it is > 1, else -1