Re: Calculate a quotient for a count of boolean values (true or false) - Mailing list pgsql-general

From Albe Laurenz
Subject Re: Calculate a quotient for a count of boolean values (true or false)
Date
Msg-id A737B7A37273E048B164557ADEF4A58B17BB1E37@ntex2010a.host.magwien.gv.at
Whole thread Raw
In response to Calculate a quotient for a count of boolean values (true or false)  (Alexander Farber <alexander.farber@gmail.com>)
List pgsql-general
Alexander Farber wrote:
> In a PostgreSQL 8.4.13 why doesn't this please
> deliver a floating value (a quotient between 0 and 1):
> 
>  select
>         id,
>         count(nullif(nice, false)) - count(nullif(nice, true)) /
> count(nice) as rating
>         from pref_rep where nice is not null
>         group by id
> ;
>            id            | rating
> -------------------------+--------
>  DE10072                 |     -1
>  DE10086                 |     18
>  DE10087                 |      1
>  DE10088                 |     -1
>  DE10095                 |    276
>  DE10097                 |     37
>  DE10105                 |      5

Because you are using integer division.

Cast one of the values to double precision, like this:

(count(nullif(nice, false)) - count(nullif(nice, true))) / count(nice)::float8

Yours,
Laurenz Albe

pgsql-general by date:

Previous
From: Alexander Farber
Date:
Subject: Re: Calculate a quotient for a count of boolean values (true or false)
Next
From: Luca Ferrari
Date:
Subject: Re: databse version