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

Поиск
Список
Период
Сортировка
От Albe Laurenz
Тема Re: Calculate a quotient for a count of boolean values (true or false)
Дата
Msg-id A737B7A37273E048B164557ADEF4A58B17BB1E37@ntex2010a.host.magwien.gv.at
обсуждение исходный текст
Ответ на Calculate a quotient for a count of boolean values (true or false)  (Alexander Farber <alexander.farber@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: Alexander Farber
Дата:
Сообщение: Re: Calculate a quotient for a count of boolean values (true or false)
Следующее
От: Luca Ferrari
Дата:
Сообщение: Re: databse version