[SQL] returning count(*) when it is > 1, else -1

2008-10-17 Thread Gerardo Herzig
Hi all. Im triyng to implement this in plain sql. The only thing i have working is select case when (select count(*) from test where id=$1 ) > 0 then (select count(*) from test where id=$1) else -1 end; But it does a doble count(*) that i must avoid. I cant refer to the 'first' count

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

2008-10-17 Thread Pavel Stehule
Hello 2008/10/17 Gerardo Herzig <[EMAIL PROTECTED]>: > Hi all. Im triyng to implement this in plain sql. > The only thing i have working is > > select case when (select count(*) from test where id=$1 ) >> 0 then (select count(*) from test where id=$1) >else -1 >end; > > But it does a d

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

2008-10-17 Thread Richard Huxton
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 FRO

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

2008-10-17 Thread Gerardo Herzig
Richard Huxton wrote: > 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 t

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

2008-10-17 Thread Achilleas Mantzios
Στις Friday 17 October 2008 15:11:10 ο/η Gerardo Herzig έγραψε: > Richard Huxton wrote: > > 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 the

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

2008-10-17 Thread Tom Lane
Richard Huxton <[EMAIL PROTECTED]> 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

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

2008-10-17 Thread Gerardo Herzig
Achilleas Mantzios wrote: > Στις Friday 17 October 2008 15:11:10 ο/η Gerardo Herzig έγραψε: >> Richard Huxton wrote: >>> 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=$

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

2008-10-17 Thread Gerardo Herzig
Tom Lane wrote: > Richard Huxton <[EMAIL PROTECTED]> 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