Re: [SQL] sum(bool)?

2001-02-26 Thread Oliver Elphick

Olaf Marc Zanger wrote:
  >hi there,
  >
  >i want to add up the "true" values of a comparison like
  >
  >sum(a>b)
  >
  >it just doesn't work like this
  >
  >any workaround?
 
select count(*) where a > b;

-- 
Oliver Elphick[EMAIL PROTECTED]
Isle of Wight  http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
 
 "But God commendeth his love toward us, in that, while 
  we were yet sinners, Christ died for us." 
   Romans 5:8 





Re: Fwd: Re: [SQL] sum(bool)?

2001-02-25 Thread D'Arcy J.M. Cain

Thus spake J.Fernando Moyano
> El Viernes 23 Febrero 2001 16:22, escribiste:
> > i want to add up the "true" values of a comparison like
> >
> > sum(a>b)
> >
> > it just doesn't work like this
> >
> > any workaround?
> 
> I did exactly the same thing two months ago 
> 
> I created this tiny function:
> 
> CREATE FUNCTION bool2int(bool) RETURNS integer
>   AS 'select (case when $1=true then 1 else 0 end)'
>   LANGUAGE 'sql';
> 
> 
> You can do:  sum(bool2int(a>b))
> 
> easy ??? ;-))

Almost as easy as "SELECT COUNT(*) FROM table WHERE a > b"

-- 
D'Arcy J.M. Cain|  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.



Fwd: Re: [SQL] sum(bool)?

2001-02-25 Thread J.Fernando Moyano


El Viernes 23 Febrero 2001 16:22, escribiste:
> hi there,
>
> i want to add up the "true" values of a comparison like
>
> sum(a>b)
>
> it just doesn't work like this
>
> any workaround?
>
> it is postgresql 7.0 under linux
>
> thanks
>
> olaf

I did exactly the same thing two months ago 

I created this tiny function:

CREATE FUNCTION bool2int(bool) RETURNS integer
AS 'select (case when $1=true then 1 else 0 end)'
LANGUAGE 'sql';


You can do:  sum(bool2int(a>b))

easy ??? ;-))


--
Fernando Moyano

Frase del día:
--
El medico me prohibio beber wiski, ahora lo congelo y me lo como!

(*) SymeX ==> http://symex.lantik.com
(*) WDBIL ==> http://wdbil.sourceforge.net
(*) Informate sobre LINUX en http://www.linux.org




[SQL] sum(bool)?

2001-02-24 Thread Olaf Marc Zanger

hi there,

i want to add up the "true" values of a comparison like

sum(a>b)

it just doesn't work like this

any workaround?

it is postgresql 7.0 under linux

thanks

olaf



-- 
soli-con Engineering Zanger, Dipl.-Ing. (FH) Olaf Marc Zanger
Lorrainestrasse 23, 3013 Bern / Switzerland
fon:+41-31-332 9782, mob:+41-76-572 9782
mailto:[EMAIL PROTECTED], http://www.soli-con.com




Re: [SQL] sum(bool)?

2001-02-23 Thread Andrew Perrin

Or how about just:

SELECT count(*) FROM tablename WHERE a > b;

--
Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology  
Chapel Hill, North Carolina, USA - http://demog.berkeley.edu/~aperrin
[EMAIL PROTECTED] - [EMAIL PROTECTED]
On Fri, 23 Feb 2001, Daniel Wickstrom wrote:

> > "Olaf" == Olaf Zanger <[EMAIL PROTECTED]> writes:
> 
> Olaf> hi there i'd like to add up the "true" values of a
> Olaf> comparison like
> 
> Olaf> sum(a>b)
> 
> Olaf> it just doesn't work like this
> 
> Olaf> any workaround
> 
> Try using a case statement:
> 
> select sum(case when  a > b then 1 else 0 end) 
> 
> 
> -Dan
> 




Re: [SQL] sum(bool)?

2001-02-23 Thread Olaf Zanger

hi there,

s cool, 
this works streight away and took 5 min. waiting for a answer :-)

thanks very much to you tod personal and the mailing list for existence.

Olaf

Tod McQuillin schrieb:
> 
> On Fri, 23 Feb 2001, Olaf Zanger wrote:
> 
> > i'd like to add up the "true" values of a comparison like
> >
> > sum(a>b)
> >
> > it just doesn't work like this
> 
> Try
> 
>  sum(case when a>b then 1 else 0 end)
> --
> Tod McQuillin

-- 
soli-con Engineering Zanger
Dipl.-Ing. (FH) Olaf Marc Zanger
Lorrainestrasse 23
3013 Bern / Switzerland
Fon: +41-31-332 9782
Mob: +41-76-572 9782
mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
http://www.soli-con.com



Re: [SQL] sum(bool)?

2001-02-23 Thread Peter Eisentraut

Olaf Zanger writes:

> i'd like to add up the "true" values of a comparison like
>
> sum(a>b)

sum(case when a>b then 1 else 0 end)

of maybe even just

select count(*) from table where a>b;

-- 
Peter Eisentraut  [EMAIL PROTECTED]   http://yi.org/peter-e/




Re: [SQL] sum(bool)?

2001-02-23 Thread Tod McQuillin

On Fri, 23 Feb 2001, Olaf Zanger wrote:

> i'd like to add up the "true" values of a comparison like
>
> sum(a>b)
>
> it just doesn't work like this

Try

 sum(case when a>b then 1 else 0 end)
-- 
Tod McQuillin





[SQL] sum(bool)?

2001-02-23 Thread Daniel Wickstrom

> "Olaf" == Olaf Zanger <[EMAIL PROTECTED]> writes:

Olaf> hi there i'd like to add up the "true" values of a
Olaf> comparison like

Olaf> sum(a>b)

Olaf> it just doesn't work like this

Olaf> any workaround

Try using a case statement:

select sum(case when  a > b then 1 else 0 end) 


-Dan



[SQL] sum(bool)?

2001-02-23 Thread Olaf Zanger

hi there

i'd like to add up the "true" values of a comparison like

sum(a>b)

it just doesn't work like this

any workaround

postgres 7.0 on linux

thanks

olaf
-- 
soli-con Engineering Zanger
Dipl.-Ing. (FH) Olaf Marc Zanger
Lorrainestrasse 23
3013 Bern / Switzerland
Fon: +41-31-332 9782
Mob: +41-76-572 9782
mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
http://www.soli-con.com