具有子句/组Gy语法错误

时间:2014-02-26 17:56:33

标签: mysql sql subquery grouping aggregate-functions

我希望选择支持代理商名称,添加到案例的标签(基于每个代理商具有相应标签的所有案例的平均评级的底部10个标签),以及该标签的平均评级。所以格式应该是

agent name | Label name  | average rating for the label on this row
agent name | label 2 name| average rating for the label in this row

我试过了:

select a.public_name, label_name, format(avg(r.rating),2) as 'average', count(label_name)
from bi.support_agents_list a 
    join bi.support_ratings_agents sra 
    on a.desk_id = sra.agent_desk_id
    join bi.support_ratings r 
    on sra.response_id = r.response_id
    and r.survey_id = sra.survey_id
    join bizdw.support_labels_assigned_v2 l
    on l.fk_case_id = r.pk_case_id
where r.date_submitted >= NOW() - INTERVAL 30 DAY
and a.start_date < '2014-01-22'
and a.end_date is null
and exists (select * 
from bi.support_ratings r2 
where r2.pk_case_id = r.pk_case_id 
having count(label_name) >= '2')
group by a.public_name, label_name
order by a.public_name, format(avg(r.rating),2), label_name desc

但是过滤掉了很多计数超过2的标签。

我知道为了将标签限制为每个代理10个,我需要一个子查询,但是当我尝试这个时:

select a.public_name, label_name, format(avg(r.rating),2) as 'average', count(label_name)
from bi.support_agents_list a 
    join bi.support_ratings_agents sra 
    on a.desk_id = sra.agent_desk_id
    join bi.support_ratings r 
    on sra.response_id = r.response_id
    and r.survey_id = sra.survey_id
    join bizdw.support_labels_assigned_v2 l
    on l.fk_case_id = r.pk_case_id
where r.date_submitted >= NOW() - INTERVAL 30 DAY
and a.start_date < '2014-01-22'
and a.end_date is null
and exists (select * 
from bi.support_ratings r2 
where r2.pk_case_id = r.pk_case_id 
having count(label_name) >= '2'
limit 10)
group by a.public_name, label_name
order by a.public_name, format(avg(r.rating),2), label_name desc

我收到语法错误:

“错误:您的SQL语法有错误;请查看手册 这对应于右边的MySQL服务器版本 在'group by a.public_name,label_name附近使用的语法 订购 通过a.public_name,格式(avg(r.rating),'在第17行 错误 代码:1064“

有什么想法?我想我需要一个子查询,但我似乎无法避免抛出错误。

0 个答案:

没有答案