1
我的產品表提供這樣替代子選擇在MySQL
Producer Category Quality
113085 EIT 05
113085 EIT 06
113085 EIT 04
113085 EIT 05
113085 EIT 05
116159 EIT 05
116159 EIT 04
116159 EIT 04
110058 EIT 06
121570 EIT 04
我想瑟每個生產者相比,平均百分比所有生產者質量等級之間的分配。 像這樣:
Producer Category Quality Totalpercent Producerpercent
113085 EIT 05 40 60
113085 EIT 06 20 20
113085 EIT 04 40 20
我有這樣的選擇做我想做的,但它周圍260.000記錄運行70-80秒,我的服務器上,這是很多:-)
你有任何提示加快這一點?
select
Producer as Producer1,
Category as Category1,
Quality as 'Quality1',
Round(((select count(*) from base where Category=Category1 and Quality=Quality1 and deliverydate >'2016-10-01')/
(select count(*) from base where Category=Category1 and deliverydate >'2016-10-01')
*100),1) as 'Totalpercent',
Round(((select count(*) from base where Category=Category1 and Quality=Quality1 and deliverydate >'2016-10-01' and Producer=Producer1)/
((select count(*) from base where Category=Category1 and deliverydate >'2016-10-01' and Producer=Producer1))
*100),1) as 'Producerpercent'
from base where deliverydate >'2016-10-01' and Producer=113085 group by Category1, Quality1