简体   繁体   中英

MySQL query inc AVG

after a bit of help please :)

The following MySQL query

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

produces the following result

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

what I want to achieve is when a posted_month contains a duplicate value I want to average the "total"

Any help would very greatly appreciated

Thanks

Just GROUP BY customer_id, user,posted_month and use AVG() function

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

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM