title: Aggregate function calls cannot be nested?
body: <p>In <code>PostgreSQL</code> database I have table called <code>answers</code>. This table stores information about how users answered a questions. There are only 4 question in the table. At the same time, the number of users who answered the questions can be dynamic and the user can answer only part of the questions.</p>
  
  <p>Table <code>answers</code>:</p>
  
  <pre><code>| EMPLOYEE | QUESTION_ID | QUESTION_TEXT          | OPTION_ID | OPTION_TEXT  |
  |----------|-------------|------------------------|-----------|--------------|
  | Bob      | 1           | Do you like soup?      | 1         | Yes          |
  | Alex     | 1           | Do you like soup?      | 2         | No           |
  | Kate     | 1           | Do you like soup?      | 3         | I don't know |
  | Bob      | 2           | Do you like ice cream? | 1         | Yes          |
  | Alex     | 2           | Do you like ice cream? | 3         | I don't know |
  | Oliver   | 2           | Do you like ice cream? | 1         | Yes          |
  | Bob      | 3           | Do you like summer?    | 2         | No           |
  | Alex     | 3           | Do you like summer?    | 1         | Yes          | 
  | Jack     | 3           | Do you like summer?    | 2         | No           |
  | Bob      | 4           | Do you like winter?    | 3         | I don't know |
  | Alex     | 4           | Do you like winter?    | 1         | Yes          |
  | Oliver   | 4           | Do you like winter?    | 3         | I don't know |
  </code></pre>
  
  <p>For example, with next code I can find average of the answers for question 1 and 2 of each person who answered for these questions.</p>
  
  <pre><code>select
      employee,
      avg(
          case when question_id in (1, 2) then option_id else null end
      ) as average_score
  from
      answers
  group by
      employee
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>| EMPLOYEE | AVERAGE_SCORE |
  |----------|---------------|
  | Bob      | 2             |
  | Alex     | 2,5           |
  | Kate     | 3             |
  | Oliver   | 1             |
  </code></pre>
  
  <p>Now, I want to know the number of users whose average of the answers for question 1 and 2 is >= than 2. I tried next code but it raise error:</p>
  
  <pre><code>select
      count(
          avg(
              case when question_id in (1, 2) then option_id else null end
          )
      ) as average_score
  from
      answers
  where
      average_score &gt;= 2
  group by
      answers.employee
  </code></pre>
  
  <p>ERROR:</p>
  
  <pre><code>SQL Error [42803]: ERROR: aggregate function calls cannot be nested
  </code></pre>