好的我有一个查询,如果array_agg = natural的第一个值我需要省略结果,所以我想我可以这样做:
select
visitor_id,
array_agg(code
order by session_start) codes_array
from mark_conversion_sessions
where conv_visit_num2 < 2
and max_conv = 1
and (array_agg(code
order by session_start))[1] != 'natural'
group by visitor_id
但是当我运行这个时,我得到错误:
ERROR: aggregate functions are not allowed in WHERE
LINE 31: and (array_agg(code
那么我可以在where子句中引用array_agg吗?
谢谢
答案 0 :(得分:1)
having
子句用于对分组数据执行where
子句。将使用聚合的条件移动到having子句中,例如:
select
visitor_id,
array_agg(code order by session_start) codes_array
from mark_conversion_sessions
where
conv_visit_num2 < 2
and max_conv = 1
group by visitor_id
having
(array_agg(code order by session_start))[1] != 'natural'
文档: https://www.postgresql.org/docs/9.6/static/tutorial-agg.html