title: SQL - calculate forecast average
body: <p>I'm trying to calculate a forecast of sales based on the 3 previous months which either can be actuals or forecast.</p>
  
  <pre><code>company_id    Year    Month    Actuals    Forecast 
    123456      2014      1         10     
    123456      2014      2         15     
    123456      2014      3         17     
    123456      2014      4                 14.00
    123456      2014      5                 15.33 
    123456      2014      6                 15.44
    123456      2014      7                 14.93
  
  Month 4 = (10+15+17)/3
  Month 5 = (15+17+14)/3
  Month 6 = (17+14+15.33)/3
  Month 7 = (14+15.33+15.44)/3
  </code></pre>
  
  <p>Let's say I want to calculate the forecast for the next 18 months for <strong>each</strong> company.</p>
  
  <p>I'm looking at the data for last year. Some companies has e.g. 2 months of data and other 12 months and so on. </p>
  
  <p>I have searched and found many different solutions but all of them only takes the actuals into account. </p>
  
  <p>I think I have to make a recursive CTE, but I can't figure it out.</p>
  
  <p>Please help :)</p>