title: Group Function is not allowed here SQL
body: <p>I have tables like this: </p>
  
  <p>Attendance(<code>offering_id</code>,visitor_id, regstr_date,amount_paid)
     Offer(<code>offering_id</code>,teacher_id)
     Teacher(<code>teacher_id</code>,teacher_firstname,teacher_lastname,start_date)</p>
  
  <p>It is possible that one  visitor may attend twice or more. I want to retrieve visitor_id, regstr_date,total amount_paid,average amount_paid  from attendance where offering id is 30,40 or 50 and teacher's start date is less than any  visitor's latest regstr_date    and    average amount_paid by per visitor is less than 600. My code is as follows:</p>
  
  <pre><code>select 
      distinct(a.visitor_id) as v_id ,
      max(a.regstr_date) as reg_date,
      sum(a.amount_paid) as total_pay,
      count(a.regstr_date) as attendance_count,
      avg(a.amount_paid) as average_paid
  from 
      attendance a, teacher t, offer o  
  where  
      a.offering_id = o.offering_id 
      and o.teacher_id = t.teacher_id 
      and a.offering_id in ('30', '40', '50') 
      and max(a.regstr_date) &gt; t.start_date
  group by 
      a.visitor_id
  having 
      avg (a.amount_paid) &lt;= 600;
  </code></pre>
  
  <p>But it shows group function is not allowed here. If possible could you help me with this please?</p>