Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>The compiler is not confused.</p> <p>The error is simply alerting you to the fact that the aggregate functions don't make sense in that context:</p> <pre><code>having count(col2) = max(count(col2) </code></pre> <p>The HAVING clause takes effect after the GROUP BY, so <code>COUNT(COL2)</code> is the count of non-null values in the <code>COL2</code> column for each distinct value of <code>col1</code>. In the context of one distinct value of <code>col1</code>, <code>MAX(COUNT(COL2))</code> makes no sense - if the result of <code>COUNT(COL2)</code> is 3, then <code>MAX(3)</code>, obviously, is 3 - but here the compiler is smart enough to know that you probably didn't intend it that way.</p> <p>So, what you were intending is that the <code>COUNT(COL2)</code> on the left-hand side was supposed to be for that distinct value of <code>COL1</code>), but the <code>COUNT(COL2)</code> on the right-hand side was supposed to be over all values of <code>COL1</code>. The expression as a whole, therefore, is a mixture and is not valid SQL.</p></li> <li><p>Yes, you can think of it that way if you like. The deepest subquery is run first, which returns a result set (in this case, a single row with a single column) to the calling subquery.</p></li> </ol> <p>You may find the following query is more efficient, instead of running effectively two queries against the table:</p> <pre><code>where t1.col1 = (select col1 from (select col1, count_col2, MAX(count_col2) OVER () max_count_col2 from (select col1, count(col2) AS count_col2 from t2 GROUP BY col1)) where count_col2 = max_count_col2) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload