2013-10-30 114 views
2

後一點幫助,請:)MySQL查詢INC AVG

下面的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包含重複值我想平均「總」

任何幫助將非常感激

感謝

回答

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 
+0

感謝VALEX!我還在學習 :) – Philwils

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