title: Aggregate function calls may not be nested ERROR
body: <p>I am trying to run this query in postgresql but getting this error: "ERROR:  Aggregate function calls may not be nested".  I am not sure what the problem or a solution for this. </p> 
  
  <p>Here is my query:</p>
  
  <pre><code>select a.KEY_NBR, a.MAIN_ID
          , 
              case
                  when a.KEY_NBR = a.MAIN_ID
                      then 'Don''t Use'
                  when a.count = MAX(a.count) over(partition by a.KEY_NBR)
                      then 'Good'
                  else 'Bad'
              end [flag]
      from MYTABLE a
  </code></pre>