0

我已经解决了这个查询:

SELECT round(avg(total_kpi),2) as avg_kpi, 
case when avg(total_kpi) < 3 then 'NEG' 
     when avg(total_kpi)>3 then 'POS' end  as kpi, 
count(gender) as gender,  
gender,  
round(avg(locacion),2) as avg_locacion, 
round(avg(tiempo),2) as avg_tiempo, 
round(avg(servicio),2) as avg_servicio, 
round(avg(calidad),2) as avg_calidad 
FROM datellig_ift.a02_view_kpi_2 
WHERE id_man_medicion=4 
group by 
case when avg(total_kpi) < 3 then 'NEG' 
     when avg(total_kpi) > 3 then 'POS' end

我需要按第 2 列对数据进行分组。分组时,mysql 拒绝。
抱歉,我一直在看一些帖子,但不明白为什么不起作用。谢谢。

错误信息:

#1111 - 组功能的使用无效

4

2 回答 2

1

AVG()不能在您的GROUP BY子句中,因为它是“分组依据”或“聚合”功能。

有关所有聚合函数的详细信息,请参阅http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

根据您的跟进,我猜这个查询将适合您:

SELECT
    IF(total_kpi < 3, 'NEG', 'POS') AS type,
    gender,  
    count(*) AS members,  
    round(avg(total_kpi),2) as avg_kpi, 
    round(avg(locacion),2) as avg_locacion, 
    round(avg(tiempo),2) as avg_tiempo, 
    round(avg(servicio),2) as avg_servicio, 
    round(avg(calidad),2) as avg_calidad 
FROM
    datellig_ift.a02_view_kpi_2 
WHERE
   id_man_medicion = 4 
GROUP BY
    total_kpi < 3, 
    gender
于 2013-05-07T22:34:16.470 回答
0

昨晚我终于找到了解决方案。由于我的评分低,不允许回答我的问题。然而,罗斯史密斯二世帮助了我,并检查了他提出的解决方案,我发现它也可以正常工作。这是我所做的:

SELECT count(total_kpi) as q_kpi, 
case when total_kpi < 3 then  'NEG'  when total_kpi>3 then  'POS'  end  as kpi, 
count(gender) as gender, 
gender, 
round(avg(locacion),2) as avg_locacion, 
round(avg(tiempo),2) as avg_tiempo, 
round(avg(servicio),2) as avg_servicio,
round(avg(calidad),2) as avg_calidad 
FROM datellig_ift.a02_view_kpi_2  where id_man_medicion=4 GROUP BY 2, 4

感谢罗斯史密斯您的评论导致找到答案。

于 2013-05-08T19:30:04.553 回答