将前几周的增长应用于未来几周

时间:2019-09-02 21:16:27

标签: sql

我正试图利用前几周的增长来预测未来的结果。

我想逐日计算过去三周的年增长率,并将其应用于上一年的未来几周的结果中,以预测我的未来结果。

--Future Data in which the average growth needs to be applied

SELECT DOW, SiteNum, dtDate, Measure1, Measure2 from tData 
    WHERE dtDate > NOW();

--Obtains growth average, by DayOfWeek, by Site    
SELECT TY.DOW, TY.SiteNum, AVG(1 + ((TY.Measure1 - LY.Measure1) / LY.Measure1)) as Measure1_GrowthAvg, AVG(1 + ((TY.Measure2 - LY.Measure2) / LY.Measure2)) as Measure2_GrowthAvg
    FROM tData TY
      INNER JOIN tData LY
          ON TY.WeekNum = LY.WeekNum
              AND TY.DOW = LY.DOW
              AND TY.SiteNum = LY.SiteNum
              AND TY.YearNum = '2019'
              AND LY.YearNum = '2018'
    WHERE TY.dtDate between DATE_ADD( NOW(), INTERVAL -21 DAY) AND NOW() 
    GROUP BY TY.DOW, TY.SiteNum
    ORDER BY TY.SITENUM, TY.DOW;

我需要将Measured1_GrowthAvg应用于第一个查询的Measure1,并将MEasured2_GrowthAvg应用于Measure2。如果DOW 1的Measured1_GrowthAvg为1.5,而同一DOW的将来Measure1为1000,则预计结果为1500。

我在dbfiddle中有示例数据:

https://www.db-fiddle.com/f/2YYhgN9aBUUFLK49g8uocX/5

第一个查询是需要应用平均增长的未来数据,第二个查询按站点的DayOfWeek返回平均增长。

0 个答案:

没有答案