这里不允许使用组函数SQL

时间:2016-01-29 05:08:50

标签: sql group-by oracle-sqldeveloper having-clause

我有这样的表:

出席(offering_id,visitor_id,regstr_date,amount_paid)    报价(offering_id,teacher_id)    老师(teacher_id,teacher_firstname,teacher_lastname,起始日期)

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

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;

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

1 个答案:

答案 0 :(得分:0)

为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;