Skip site navigation (1) Skip section navigation (2)

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

From: Richard Huxton <dev(at)archonet(dot)com>
To: Gerardo Herzig <gherzig(at)fmed(dot)uba(dot)ar>
Cc: postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: returning count(*) when it is > 1, else -1
Date: 2008-10-17 12:01:14
Message-ID: 48F87E8A.1080103@archonet.com (view raw or flat)
Thread:
Lists: pgsql-sql
Gerardo Herzig wrote:
> 
> But it does a doble count(*) that i must avoid.
> I cant refer to the 'first' count like
> select case when (select count(*) from test where id=$1 ) AS total
>     > 0 then total
>     else -1
>     end;

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

-- 
  Richard Huxton
  Archonet Ltd

In response to

Responses

pgsql-sql by date

Next:From: Gerardo HerzigDate: 2008-10-17 12:11:10
Subject: Re: returning count(*) when it is > 1, else -1
Previous:From: Pavel StehuleDate: 2008-10-17 12:00:55
Subject: Re: returning count(*) when it is > 1, else -1

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group