title: SQL Server - Cumulative Sum over Last 12 Months, but starting from the Last Month (SQL Server 18)
body: <p>I need to run a cumulative sum of a value over the Last 12 Months. So far, my cumulative calculation are working, but starting from the Current Month.
  I need the total of Last 12 Months, starting <strong>from the Last Month</strong>.
  Currently, I'm using <code>OVER</code> clause on SQL, starting to running the cumulative total from the current row/month.</p>
  
  <p>Please, refer below my code example:</p>
  
  <pre><code>SELECT  *,
           SUM(Amount) OVER (PARTITION BY ID ORDER BY Date_Month ROWS BETWEEN 11 PRECEDING AND CURRENT ROW) AS TwelveMoTtl
           FROM ( 
      SELECT DISTINCT
          CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, TransactionDt), 0) AS DATE)   AS Date_Month,
          ID,
          SUM(Amount) AS Amount
      FROM MyTable
      WHERE TransactionDt &gt;= '2019-01-01'
      GROUP BY
          ID,
          CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, TransactionDt), 0) AS DATE)
  </code></pre>
  
  <p>Here is my results (using only one ID to simplify the example):</p>
  
  <p><img src="https://i.stack.imgur.com/MpkDBm.png" alt="enter image description here"></p>
  
  <p>As my example, the calculation are starting from the current row, and running over the last 12 months. </p>
  
  <p>If we take the February row for example, I need the cumulative sum from Jan, 2020 to February, 2019.</p>
  
  <p>Any suggestions how could I do it?</p>
  
  <p>Thanks,</p>