SQL Select Statement For Calculating A Running Average Column

前端 未结 7 861
-上瘾入骨i
-上瘾入骨i 2020-12-11 04:53

I am trying to have a running average column in the SELECT statement based on a column from the n previous rows in the same SELECT statement. The average I need is based on

相关标签:
7条回答
  • 2020-12-11 05:45

    Edit: I missed the point that it should average the three previous records...

    For a general running average, I think something like this would work:

    SELECT
        id, number, 
        SUM(number) OVER (ORDER BY ID) / 
           ROW_NUMBER() OVER (ORDER BY ID) AS [RunningAverage]
    FROM myTable
    ORDER BY ID
    
    0 讨论(0)
提交回复
热议问题