SELECT
f.*,
q.day_avg_gmv
FROM
fact_purchases AS f
JOIN (
SELECT
ds,
AVG(gmv) AS day_avg_gmv
FROM
fact_purchases
WHERE
istest = FALSE
GROUP BY
ds
) AS q
ON q.ds = f.ds
WHERE
f.istest = FALSE
ORDER BY
f.ds
HAVING
f.gmv > q.day_avg_gmv
You can see the statement. I've tried to filter the gmv that is more than average. However, I've got this error:
ERROR: syntax error at or near "HAVING"
I know that I can solve the problem using this condition f.gmv > q.day_avg_gmv
in WHERE, but I want to know why it's not working in HAVING.