此处不允许组函数 SQL

Group Function is not allowed here SQL

我有这样的表格:

出勤率(offering_id,visitor_id,regstr_date,amount_paid) 报价(offering_id,teacher_id) 老师(teacher_id,teacher_firstname,teacher_lastname,start_date)

一位访客可能会参加两次或更多次。我想从出勤中检索 visitor_id、regstr_date、总计 amount_paid、平均 amount_paid,其中提供的 ID 为 30,40 或 50,并且教师的开始日期小于任何访客的最新开始日期regstr_date 平均每个访问者 amount_paid 不到 600。我的代码如下:

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) > t.start_date
group by 
    a.visitor_id
having 
    avg (a.amount_paid) <= 600;

但显示此处不允许使用群组功能。如果可能的话,你能帮我解决这个问题吗?

此查询是为 MS-SQL 服务器编写的

select  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 Inner Join  offer o   on a.offering_id = o.offering_id
inner join  teacher t on t.teacher_id = o.teacher_id
where a.offering_id in('30','40','50') 
group by a.visitor_id,t.start_date
having avg (a.amount_paid)<=600 and  max(a.regstr_date)>t.start_date;