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

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: returning count(*) when it is > 1, else -1
Дата
Msg-id 14252.1224246752@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: returning count(*) when it is > 1, else -1  (Richard Huxton <dev@archonet.com>)
Ответы Re: returning count(*) when it is > 1, else -1
Список pgsql-sql
Richard Huxton <dev@archonet.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


В списке pgsql-sql по дате отправления:

Предыдущее
От: Achilleas Mantzios
Дата:
Сообщение: Re: returning count(*) when it is > 1, else -1
Следующее
От: Gerardo Herzig
Дата:
Сообщение: Re: returning count(*) when it is > 1, else -1