我正在尝试划分以下内容:
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
如果我从除法更改为加法,减法等,我会得到正确的答案,只是由于某些原因,除法是错误的。
答案 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;