计算移动平均值作为子查询

时间:2019-10-08 21:52:33

标签: mysql sql

我需要在制造指标数据库中为总废品价格创建26周移动平均值。当前系统有4个数据源,因此我为每个数据源创建了一个表。然后,我有一个子查询,它遍历每个表,合并表之间的所有数据,并报告按生产周汇总的信息。

我尝试结合在另一个站点上找到的这个示例:

DROP TABLE IF EXISTS t;
CREATE TABLE t (dt DATE, qty INT);
INSERT INTO t VALUES ('2007-1-1',5),
                     ('2007-1-2',6),
                     ('2007-1-3',7),
                     ('2007-1-4',8),
                     ('2007-1-5',9),
                     ('2007-1-6',10),
                     ('2007-1-7',11),
                     ('2007-1-8',12),
                     ('2007-1-9',13);

SELECT 
  t1.dt, 
  ( SELECT SUM(t2.qty) / COUNT(t2.qty)
    FROM t AS t2
    WHERE DATEDIFF(t1.dt, t2.dt) BETWEEN 0 AND 4
  ) AS '5dayMovingAvg'
FROM t AS t1
ORDER BY t1.dt;

我能够合并该代码,但是我必须运行两次大型查询才能获得正确的结果(一次在选择区域中,再一次从中)。有没有更简单的方法可以做到这一点?

SELECT 
  date_format(t1.date, '%Y') as year, date_format(t1.date, '%m') as month, date_format(t1.date, '%d') as day, t1.epc, t1.scrap, t1.freight, t1.smo, t1.ext_sort, t1.total, 
  ( SELECT SUM(t2.total) / COUNT(t2.total)
    FROM 

   (SELECT date, sum(epc_labor_cost) as epc, sum(scrap_value) as scrap, sum(prem_freight_cost) as freight, sum(smo_sort_hours) as smo, sum(ext_sort) as ext_sort, sum(epc_labor_cost+scrap_value+prem_freight_cost+smo_sort_hours+ext_sort) as total FROM (
                                           select pn, date, labor_cost as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, 0 as ext_sort, 0 as total from epc_data 
                                 union all select pn, date, 0 as epc_labor_cost, abs(value) as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, 0 as ext_sort, 0 as total from scrap 
                                 union all select pn, date, 0 as epc_labor_cost, 0 as scrap_value, cost as prem_freight_cost, 0 as smo_sort_hours, 0 as ext_sort, 0 as total from prem_freight 
                                 union all select pn, date, 0 as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, hours*4.02 as smo_sort_hours, 0 as ext_sort, 0 as total from smo_sort
                                union all select STRIP_NON_DIGIT(ext_sort.pn) as pn, date, 0 as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, (ext_sort.sorted*pn_data.ext_sort_cost) as ext_sort, 0 as total from ext_sort inner join pn_data on STRIP_NON_DIGIT(ext_sort.pn)=STRIP_NON_DIGIT(pn_data.pn) ) as test group by year(date), week(date,3) ORDER BY date desc) as t2
    WHERE DATEDIFF(t1.date, t2.date) BETWEEN 0 AND 92
  ) AS movavg
FROM (SELECT date, sum(epc_labor_cost) as epc, sum(scrap_value) as scrap, sum(prem_freight_cost) as freight, sum(smo_sort_hours) as smo, sum(ext_sort) as ext_sort, sum(epc_labor_cost+scrap_value+prem_freight_cost+smo_sort_hours+ext_sort) as total FROM (
                                           select pn, date, labor_cost as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, 0 as ext_sort, 0 as total from epc_data 
                                 union all select pn, date, 0 as epc_labor_cost, abs(value) as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, 0 as ext_sort, 0 as total from scrap 
                                 union all select pn, date, 0 as epc_labor_cost, 0 as scrap_value, cost as prem_freight_cost, 0 as smo_sort_hours, 0 as ext_sort, 0 as total from prem_freight 
                                 union all select pn, date, 0 as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, hours*4.02 as smo_sort_hours, 0 as ext_sort, 0 as total from smo_sort
                                union all select STRIP_NON_DIGIT(ext_sort.pn) as pn, date, 0 as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, (ext_sort.sorted*pn_data.ext_sort_cost) as ext_sort, 0 as total from ext_sort inner join pn_data on STRIP_NON_DIGIT(ext_sort.pn)=STRIP_NON_DIGIT(pn_data.pn) ) as test group by year(date), week(date,3) ORDER BY date desc) AS t1
ORDER BY t1.date desc limit

0 个答案:

没有答案