MySQL 查询公司 AVG

标签 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"       |

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

任何帮助将不胜感激

谢谢

最佳答案

只需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

关于MySQL 查询公司 AVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19680550/

相关文章:

c# - SQL 主键生成

mysql - 在 Yii 中设置时区

MySQL 存储过程无法产生结果

MySQL 内部连接查询没有返回正确的结果

php - mysql 将文本输入转换为日期时间字段

mysql - 模糊自动完成

c# - 使用 C# 从 MySQL 数据库中的 JSON 响应插入日期时间

php - 如何使用 LIKE 忽略 mysql 搜索查询中的空格?

php - 为什么 CakePHP 中的创建函数试图保存以前的条目?导致重复输入错误

mysql - 选择串口和IP,但IP相同的行优先