PostgreSQL:使用过滤子句的语法错误

PostgreSQL: Syntax error using the filter clause

在查询的一部分中,我希望计算每个学生每次考试花费的平均时间。但是,我在知道在哪里放置“过滤器”时遇到语法困难。

通常,将其放在聚合函数工作之后 - 例如

count(distinct student_id) filter (where student_id is not null or student_id not in ('A', 'C', 'Z'))

但是,下面的方法不起作用。我继续移动它,但还没有运气。我可以解释一下如何做对吗?提前致谢!

 ( sum(extract(epoch from time_spent)) / count(distinct exam_id)::float )/60
          filter (where student_id is not null or student_id not in ('A', 'C', 'Z'))
     as avg_time_min,

考虑 运行 FILTER() 表达式的两个聚合,并在计算后调整转换为

SELECT ...
       
       sum(extract(epoch from time_spent)) filter (
           where student_id is not null or student_id not in ('A', 'C', 'Z')
       )          
       /        
       count(distinct exam_id) filter (
           where student_id is not null or student_id not in ('A', 'C', 'Z')
       )::float/60 
       
       AS avg_time_min
       ...

FROM my_table