Re: [SQL] sum-function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: guenther(at)laokoon(dot)IN-Berlin(dot)DE (Christian Guenther)
Cc: pgsql-sql(at)hub(dot)org
Subject: Re: [SQL] sum-function
Date: 1999-10-23 04:55:35
Message-ID: 799.940654535@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

guenther(at)laokoon(dot)IN-Berlin(dot)DE (Christian Guenther) writes:
> udmsearch=> select sum( dict.word = 'mysql') from dict\g
> ERROR: Unable to select an aggregate function sum(bool)
> What does the ERROR mean.

The '=' operator produces a boolean (true/false) result, but
sum() wants a numeric input. Postgres is a strongly-typed
system, so it doesn't think booleans are interchangeable
with numerics.

It's been suggested several times that Postgres should
provide a standard conversion function that converts
boolean values to 0/1 numeric values, but no one's gotten
around to making one. In the meantime you could get the
result that I think you want with something like

select sum(case when dict.word = 'mysql' then 1 else 0 end) ...

regards, tom lane

Browse pgsql-sql by date

  From Date Subject
Next Message Albert REINER 1999-10-23 07:18:19 Can VACUUM, but cannot do VACUUM ANALYZE
Previous Message Christian Guenther 1999-10-22 23:34:51 sum-function