SQL Select Statement For Calculating A Running Average Column

前端 未结 7 915
-上瘾入骨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:37

    If you want this to be truly performant, and arn't afraid to dig into a seldom-used area of SQL Server, you should look into writing a custom aggregate function. SQL Server 2005 and 2008 brought CLR integration to the table, including the ability to write user aggregate functions. A custom running total aggregate would be the most efficient way to calculate a running average like this, by far.

提交回复
热议问题