PostgreSQL除法运算不正确

时间:2018-07-17 01:57:32

标签: sql postgresql

我正在尝试划分以下内容:

SELECT                                                                                                              
(count(1) filter (where status = 'Failed') / 
count(2) filter (where id > 0))
from tracking;

表中的数据如下:

id  status
1   Failed
2   Failed  
3   Failed
4   Success

因此,结果应该是; .75,但我得到以下信息:

-[ RECORD 1 ]
?column? | 0

如果我从除法更改为加法,减法等,我会得到正确的答案,只是由于某些原因,除法是错误的。

1 个答案:

答案 0 :(得分:1)

您需要一个小数点:

select (count(1) filter (where status = 'Failed') * 1.0 / 
        count(1) filter (where id > 0)
       )
from tracking;

但是,我怀疑这会满足您的要求:

select avg( (status = 'Failed'::int) )
from tracking
where id > 0;