Re: [SQL] sum-function - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] sum-function
Date
Msg-id 799.940654535@sss.pgh.pa.us
Whole thread Raw
In response to sum-function  (guenther@laokoon.IN-Berlin.DE (Christian Guenther))
List pgsql-sql
guenther@laokoon.IN-Berlin.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


pgsql-sql by date:

Previous
From: guenther@laokoon.IN-Berlin.DE (Christian Guenther)
Date:
Subject: sum-function
Next
From: "Albert REINER"
Date:
Subject: Can VACUUM, but cannot do VACUUM ANALYZE