title: SQL First In First Out Loyalty Point
body: <p>fellow developers and analysts. I have some experience in SQL and have resorted to similar posts. However, this is slightly more niche. Thank you in advance for helping.</p>
  
  <p>I have the below dataset  (edited. Apology)</p>
  
  <p><a href="https://i.stack.imgur.com/q9Uu3.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/q9Uu3.jpg" alt="Data Set"></a></p>
  
  <blockquote>
    <p>Setup</p>
  </blockquote>
  
  <pre><code>CREATE TABLE CustomerPoints
  (
      CustomerID INT,
      [Date]     Date,
      Points     INT
  )
  
  INSERT INTO CustomerPoints
  VALUES
      (1, '20150101', 500),
      (1, '20150201', -400),
      (1, '20151101', 300),
      (1, '20151201', -400)
  </code></pre>
  
  <p>and need to turn it into (edited. The figures in previous table were incorrect)
  <a href="https://i.stack.imgur.com/0McmK.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/0McmK.jpg" alt="enter image description here"></a></p>
  
  <p>Any positive amount of points are points earned whereas negative are redeemed. Because of the FIFO (1st in 1st out concept), of the second batch of points spent (-400), 100 of those were taken from points earned on 20150101 (UK format) and 300 from 20151101.</p>
  
  <p>The goal is to calculate, for each customer, the number of points spent within x and y months of earning. Again, thank you for your help.</p>