title: Compare counts in PostgreSQL
body: <p>I want to compare two results of queries of the same table, by checking theresulting row count, but Postgres doesn't support column aliases in the where clause.</p>
  
  <pre><code>select id from article where version=1308
  and exists(
  select count(ident) as count1 from artprice AS p1
  where p1.valid_to&lt;to_timestamp(1586642400000) or p1.valid_from&gt;to_timestamp(1672441199000)
    and p1.article=article.id
    and p1.count1=(select count(ident) from artprice where article=article.id)
  )
  </code></pre>
  
  <p>I also cannot use aggregate functions in the where clause, so </p>
  
  <pre><code>select id from article where version=1308
  and exists(
  select count(ident) as count1 from artprice AS p1
  where p1.valid_to&lt;to_timestamp(1586642400000) or p1.valid_from&gt;to_timestamp(1672441199000)
    and p1.article=article.id
    and p1.count(ident)=(select count(ident) from artprice where article=article.id)
  )
  </code></pre>
  
  <p>also doesn't work. Any ideas?</p>
  
  <p>EDIT:
  What I want to get are articles where every article price is outside of a valid range defined by validFrom andValidTo.</p>