计算数据集的移动平均值

时间:2019-07-24 02:36:09

标签: u-sql

下面是我的数据集的示例表示,我想计算给定日期的给定部门,子组和团队的总员工移动7天平均值。

enter image description here

是否可以通过某种方式将此行集传递给可以计算移动平均值的C#方法?还有另一种更有效的方法可以做到这一点。

1 个答案:

答案 0 :(得分:1)

根据您的数据,我进行了以下查询:

    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 
;

请告诉我您是否要实现!