Hi Experts!
Your support required to get the forecast values based on average of last available month values and continues forecasting till end of the month. Suppose i have 3days data available of the month the sql make an average of last three days and keep the average till last date of the month. If the data inserted on 4day of the month then take last 4days of average and continues till end of the month.
My try I am able to get the averages of last 3days ,
Required sqlquery to look every time first date of the month and calculate forcasts for next days till end of the month .
attaching current results and required results
Current Results from sample data and Sqlquery.
Required results in table we have last 4days avaiable next dates are foracast dates and values.
DROP TABLE IF EXISTS #Table;
GO
SELECT StartDate = CONVERT(DATETIME ,v.StartDate)
,ItemName = CONVERT(VARCHAR(1000),v.ItemName)
,Data1 = CONVERT(FLOAT ,v.Data1)
,Data2 = CONVERT(FLOAT ,v.Data2)
INTO #Table
FROM (VALUES
('1-Oct-2021','ItemName1',1336.9 ,0.85623)
,('1-Oct-2021','ItemName2',318.62 ,0.31913)
,('1-Oct-2021','ItemName3',40 ,50 )
,('2-Oct-2021','ItemName1',10 ,20 )
,('2-Oct-2021','ItemName2',11 ,21 )
,('2-Oct-2021','ItemName3',12 ,22 )
,('3-Oct-2021','ItemName1',10 ,20 )
,('3-Oct-2021','ItemName2',11 ,21 )
,('3-Oct-2021','ItemName3',12 ,22 )
,('4-Oct-2021','ItemName1',100 ,220 )
,('4-Oct-2021','ItemName2',112 ,221 )
,('4-Oct-2021','ItemName3',124 ,422 )
)v(StartDate,ItemName,Data1,Data2)
SELECT * FROM #Table
order by StartDate
--declare @avgdays as int
--set @avgdays =3
SELECT *,
[Data1AvgLast3days] = avg(Data1) OVER(PARTITION BY ItemName ORDER BY StartDate ROWS BETWEEN 3 PRECEDING AND CURRENT ROW),
[Data2AvgLast3days] = avg(Data2) OVER(PARTITION BY ItemName ORDER BY StartDate ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)
FROM #Table where ItemName='ItemName1';