MySQL:如何基于记录的每个日期的前5个记录来平均一列?

时间:2016-08-31 09:06:17

标签: mysql average moving-average

示例:

源列是fld_daily_val,fld_date

表名:tbl_five_days_avg

id fld_daily_val    fld_date

1  3.3658974569     2016-02-01                                                                                   
2  2.215478659      2016-02-02                                                             
3  1.25984453412    2016-02-03                                               
4  0.2589631477     2016-02-04                                                       
5  9.2587894561     2016-02-05                                                                                                          
6  7.2587415935     2016-02-06                                                    
7  6.589789789      2016-02-07                                                        
8  5.3654951357     2016-02-08

输出列名称为fld_five_days_avg。我们希望输出是以下

id    fld_daily_val    fld_date      fld_five_days_avg

1     3.3658974569     2016-02-01    3.3658974569   --No data before this date.                                                                 
2     2.215478659      2016-02-02    2.79068805795000 --id(1+2)/2                                                                        
3     1.25984453412    2016-02-03    2.280406883340000 --id(1+2+3)/3                                                                     
4     0.2589631477     2016-02-04    1.775045949430000 --id(1+2+3+4)/4                                                                        
5     9.2587894561     2016-02-05    3.271794650764000  --id(1+2+3+4+5)/5
6     7.2587415935     2016-02-06    7.2587415935 --should consider this is the first record after five record average completed.                                                            
7     6.589789789      2016-02-07    6.92426569125000 --id(6+7)/2                                                      
8     5.3654951357     2016-02-08    6.40467550606667 --id(6+7+8)/3

帮助我们提高知识水平。提前致谢

谢谢,

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT 
    id,
    fld_daily_val,
    fld_date,
    @idcount:=CASE
        WHEN @idcount = 5 THEN 1
        ELSE @idcount + 1
    END,
    (@sumavg:=CASE
        WHEN @idcount = 1 THEN fld_daily_val
        ELSE fld_daily_val + @sumavg
    END) / @idcount fld_five_days_avg
FROM
    (SELECT @idcount :=0, @sumavg := 0.0) a,
    tbl_five_days_avg;

<强>输出:

1   3.365897456900000   2016-02-01  1   3.3658974569
2   2.215478659000000   2016-02-02  2   2.7906880579499997
3   1.259844534120000   2016-02-03  3   2.28040688334
4   0.258963147700000   2016-02-04  4   1.77504594943
5   9.258789456100000   2016-02-05  5   3.271794650764
6   7.258741593500000   2016-02-06  1   7.2587415935
7   6.589789789000000   2016-02-07  2   6.92426569125
8   5.365495135700000   2016-02-08  3   6.404675506066667

更新逻辑

SELECT 
    id,
    fld_daily_val,
    fld_date,
    @idcount:=@idcount + 1,
    @firstval:=@secondval,
    @secondval:=@thirdval,
    @thirdval:=@fourthval,
    @fourthval:=@fifthval,
    @fifthval:=fld_daily_val,
    (@firstval + @secondval + @thirdval + @fourthval + @fifthval) / (CASE
        WHEN @idcount > 5 THEN 5
        ELSE @idcount
    END) fld_five_days_avg
FROM
    (SELECT 
        @idcount:=0,
            @firstval:=0.0,
            @secondval:=0.0,
            @thirdval:=0.0,
            @fourthval:=0.0,
            @fifthval:=0.0
    ) a,
    tbl_five_days_avg;