MySQL查询公司AVG

时间:2013-10-30 11:07:43

标签: mysql

在获得一些帮助之后,请:)

以下MySQL查询

select id, customer_id, user,
(((q_responce_time + o_responce_time + cs_responce_time + q_accuracy + 
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel + 
q_communication + o_communication + d_communication + cs_communication + 
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile + 
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100) as total,
month(create_datetime) as posted_month 
from cs_review_centre
Where
create_datetime >= '2013-01-01' 
and create_datetime < '2013-10-31'
and customer_id = 26
order by posted_month, customer_id

产生以下结果

"customer_id"  | "user"     |"total"    |"posted_month"|
"26"           | "co2_test" |"72.8000"  |   "7"        |
"26"           | "co2_test" |"60.8000"  |   "8"        |
"26"           | "Lisa"     |"81.6000"  |   "9"        |
"26"           | "Lisa"     |"84.0000"  |   "10"       |
"26"           | "Lisa"     |"52.0000"  |   "10"       |

我想要实现的是当publish_month包含重复值时,我想平均“总计”

非常感谢任何帮助

谢谢

2 个答案:

答案 0 :(得分:0)

只需GROUP BY customer_id, user,posted_month并使用AVG()函数

select customer_id, user,
AVG(
(((q_responce_time + o_responce_time + cs_responce_time + q_accuracy + 
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel + 
q_communication + o_communication + d_communication + cs_communication + 
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile + 
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100)
) as AverageTotal,
month(create_datetime) as posted_month 
from cs_review_centre
Where
create_datetime >= '2013-01-01' 
and create_datetime < '2013-10-31'
and customer_id = 26

GROUP BY customer_id, user,posted_month 

order by posted_month, customer_id

答案 1 :(得分:0)

尝试:

select id, customer_id, user,
avg((((q_responce_time + o_responce_time + cs_responce_time + q_accuracy + 
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel + 
q_communication + o_communication + d_communication + cs_communication + 
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile + 
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100)) as total,
month(create_datetime) as posted_month 
from cs_review_centre
Where
create_datetime >= '2013-01-01' 
and create_datetime < '2013-10-31'
and customer_id = 26
group by id, customer_id, user, month(create_datetime)
order by posted_month, customer_id