Thread: sum(bool)?

sum(bool)?

From
Olaf Zanger
Date:
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:info@soli-con.com
mailto:olaf.zanger@soli-con.com
http://www.soli-con.com


sum(bool)?

From
Daniel Wickstrom
Date:
>>>>> "Olaf" == Olaf Zanger <olaf.zanger@soli-con.com> 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: sum(bool)?

From
Tod McQuillin
Date:
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




Re: sum(bool)?

From
Peter Eisentraut
Date:
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      peter_e@gmx.net       http://yi.org/peter-e/



Re: sum(bool)?

From
Olaf Zanger
Date:
hi there,

soooo 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:info@soli-con.com
mailto:olaf.zanger@soli-con.com
http://www.soli-con.com


Re: sum(bool)?

From
Andrew Perrin
Date:
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       aperrin@socrates.berkeley.edu -
aperrin@igc.apc.org
On Fri, 23 Feb 2001, Daniel Wickstrom wrote:

> >>>>> "Olaf" == Olaf Zanger <olaf.zanger@soli-con.com> 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
>