Search Postgresql Archives

Re: Calculate a quotient for a count of boolean values (true or false)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general





[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux