滚动平均SQL

时间:2018-11-28 07:20:40

标签: sql average

嗨,我有一个数据集,其中有“年月”和输出变量,其值如下:

Year    |      Month      |    Output
2015    |       1         |     12
2015    |       2         |     24
2015    |       3         |     2
2015    |       4         |     3
2015    |       5         |     7
2015    |       6         |     3
2015    |       7         |     7
2015    |       8         |     6
2015    |       9         |     7
2015    |      10         |     8
2015    |      11         |     3
2015    |      12         |     6
2016    |      1          |     3
2016    |      2          |     6
2016    |      3          |     8
2016    |      4          |     9
2016    |      5          |     4

.........等等...

我想在数据集中添加一个新列作为Rolling_Average Rolling_Average =前12个月总和/本月总产出

例如: Rolling_Average(for 2015-7)=产出(2015-01)+产出(2015-02)+产出(2015-03)+产出(2015-04)+产出(2015-05)+产出(2015-06)/输出(2015-07)

我尝试了一些在线查询来获取输出,但是对我来说不起作用。有人可以帮我吗

要求的输出如下:

Year    |      Month      |    Output  |  Rolling Average
2015    |       1         |     12     |    12
2015    |       2         |     24     |    0.5 
2015    |       3         |     2      |    18
2015    |       4         |     3      |    38/3
2015    |       5         |     7      |    45/7
2015    |       6         |     3      |    48/3
2015    |       7         |     7      |    55/7
2015    |       8         |     6      |    61/6
2015    |       9         |     7      |    68/7
2015    |      10         |     8      |    74/8
2015    |      11         |     3      |    77/3
2015    |      12         |     6      |    83/6
2016    |      1          |     3      |    86/3
2016    |      2          |     6      |    92/6
2016    |      3          |     8      |    100/8
2016    |      4          |     9      |    109/9
2016    |      5          |     4      |    113/4

我尝试过的查询是:

SELECT DISTINCT
    //CALCULATIONS
   Year,
   Month,
   Output,
   (sum(CAST(Output) AS DOUBLE)))
   over(order by  year,month   rows  between 12 preceding and 1 preceding )
   as Rolling_Average

   from my_table
   group by  Year,Month
   order by  Year,Month

它给我错误: 语法错误:OVER关键字必须在函数调用之后

我还尝试了其他方法

有人可以简单地帮助我吗?我正在使用SQL Plx,它类似于SQL

谢谢!

2 个答案:

答案 0 :(得分:1)

您可能放错了一些括号

(sum( CAST(Output) AS DOUBLE ))) over (order by year, month rows between 12 preceding and 1 preceding ) as Rolling_Average

对:

SUM( CAST(Output AS DOUBLE) ) OVER (order by year, month rows between 12 preceding and 1 preceding) as Rolling_Average

您也可以舍入该结果。

这些记录似乎在年份和月份方面是独一无二的。
因此,实际上并不需要对它们进行分组。

SELECT 
 t.Year, t.Month, t.Output,
 ROUND(SUM(CAST(t.Output AS INT)) OVER (ORDER BY t.Year, t.Month ROWS BETWEEN 12 PRECEDING AND 1 PRECEDING)*1.0 / CAST(t.Output AS INT), 1)  as Rolling_Average
FROM my_table t
ORDER BY t.Year, t.Month;

如果不支持窗口功能,则可以使用:

SELECT 
 t1.Year, t1.Month, t1.Output,
 ROUND(SUM(CAST(t2.Output AS INT))*1.0 / CAST(t1.Output AS INT), 1) as Rolling_Average
FROM my_table t1
LEFT JOIN my_table t2 ON ((t2.Year = t1.Year AND t2.Month < t1.Month) OR 
                          (t2.Year = t1.Year - 1 AND t2.Month >= t1.Month))
GROUP BY t1.Year, t1.Month, t1.Output
ORDER BY t1.Year, t1.Month;

db <>提琴here

答案 1 :(得分:0)

尝试一下(如果使用sql-server)

Select * 
from tableName T
outer apply (
             select sum(output) Rolling_Average 
             from tableName T_in on T_in.year = T.year and T_in.Month <= T.Month 
            )x