title: SQL Server : moving average calculation
body: <p>I have a script that calculates moving avg based on the past 3 months.</p>
  
  <p>Is it possible to modify this moving average to display values from only the 4th month ? Or when I want to calculate based on 4 months then display values from the 5th row and so on...</p>
  
  <p>So the point is to display values only from that month when I have enough data for the calculation.</p>
  
  <pre><code>SELECT 
      [Brand], [month],
      CAST(SUM([Volume]) AS INT) AS [Volume (t)],
      AVG(CAST(SUM([Volume]) AS INT)) OVER (PARTITION BY [Brand]) AS [Average (t)],
      AVG(CAST(SUM([Volume]) AS INT)) OVER (PARTITION BY [Brand] ORDER BY [month] ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING) AS [Moving Average (3)]
  FROM 
      dw_lc_full_aggregated_1
  WHERE   
      [year] = 2016 AND 
      [month] BETWEEN 1 AND 9 AND
      [Navision Source] = 'HU' AND
      [Brand] = 'xxxxxxx'
  GROUP BY 
      [Brand], [month]
  </code></pre>