title: I don't understand why I cannot use HAVING in this statement
body: <pre><code>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 &gt; q.day_avg_gmv
  </code></pre>
  <p>You can see the statement. I've tried to filter the gmv that is more than average. However, I've got this error:</p>
  <blockquote>
  <p>ERROR: syntax error at or near &quot;HAVING&quot;</p>
  </blockquote>
  <p>I know that I can solve the problem using this condition <code>f.gmv &gt; q.day_avg_gmv</code> in WHERE, but I want to know why it's not working in HAVING.</p>