计算数据集的移动平均值
Calculate moving average of a dataset
我的数据集的示例表示如下,我想计算给定日期一组给定部门、子组和团队的员工总数的移动 7 天平均值。
有什么方法可以将此行集传递给可以计算移动平均值的 C# 方法吗?还有其他更有效的方法吗?
根据您的数据,我做了以下查询:
SELECT * FROM
(VALUES
(new DateTime(2019,01,01),"D1","S1","T1",20),
(new DateTime(2019,01,02),"D1","S1","T1",33),
(new DateTime(2019,01,03),"D1","S1","T1",78),
(new DateTime(2010,01,05),"D1","S2","T2",77)
) AS T(date,Dept, Subgroup, Team, Total_Employees);
@moving_Average_Last_7_Days =
SELECT DISTINCT
current.date,
current.Dept,
current.Subgroup,
current.Team,
current.Total_Employees,
AVG(lasts.Total_Employees) OVER(PARTITION BY lasts.Dept, lasts.Subgroup, lasts.Team,current.date) AS Moving_Average_Last_7_Days
FROM @moving_Average_Last_7_Days AS current
CROSS JOIN
@moving_Average_Last_7_Days AS lasts
WHERE (current.date - lasts.date).Days BETWEEN 0 AND 6
;
请告诉我这是否是您想要实现的!
我的数据集的示例表示如下,我想计算给定日期一组给定部门、子组和团队的员工总数的移动 7 天平均值。
有什么方法可以将此行集传递给可以计算移动平均值的 C# 方法吗?还有其他更有效的方法吗?
根据您的数据,我做了以下查询:
SELECT * FROM
(VALUES
(new DateTime(2019,01,01),"D1","S1","T1",20),
(new DateTime(2019,01,02),"D1","S1","T1",33),
(new DateTime(2019,01,03),"D1","S1","T1",78),
(new DateTime(2010,01,05),"D1","S2","T2",77)
) AS T(date,Dept, Subgroup, Team, Total_Employees);
@moving_Average_Last_7_Days =
SELECT DISTINCT
current.date,
current.Dept,
current.Subgroup,
current.Team,
current.Total_Employees,
AVG(lasts.Total_Employees) OVER(PARTITION BY lasts.Dept, lasts.Subgroup, lasts.Team,current.date) AS Moving_Average_Last_7_Days
FROM @moving_Average_Last_7_Days AS current
CROSS JOIN
@moving_Average_Last_7_Days AS lasts
WHERE (current.date - lasts.date).Days BETWEEN 0 AND 6
;
请告诉我这是否是您想要实现的!