postgres:将filter()与percentile_cont()组合

时间:2018-11-19 19:25:12

标签: postgresql

尝试在对filter()的调用中使用percentile_const子句,但不确定是否可以这样做。有办法吗?这是查询示例:

select
    count(*) as n1,
    count(*) filter(where ha >= 0) as n2,
    percentile_cont(.9) within group (order by es asc) as p1,
    percentile_cont(.9) filter (where ha >= 0) within group (order by es asc) as p2
from mytable where mypid = 123;

该查询在没有p2调用的情况下仍然可以正常工作,但是您可以看到我想做什么。

1 个答案:

答案 0 :(得分:1)

filter必须放在within group部分之后:

select
    count(*) as n1,
    count(*) filter(where ha >= 0) as n2,
    percentile_cont(.9) within group (order by es asc) as p1,
    percentile_cont(.9) within group (order by es asc) filter (where ha >= 0) as p2
from mytable 
where mypid = 123;