title: Running total SQL Server query
body: <p>So far, I have the following SQL Server 2005 query:</p>
  <pre><code>WITH D AS (
  SELECT CONVERT(VARCHAR, '2020.11.01', 102) AS d_y_m, CAST('2020-11-01' AS DATETIME) AS dt
  UNION ALL
  SELECT CONVERT(VARCHAR, DATEADD(dd, 1, z.dt), 102) AS d_y_m, DATEADD(dd, 1, z.dt)
  FROM D AS z
  WHERE DATEADD(dd, 1, z.dt) &lt;= '2020-11-30')
  SELECT x.d_y_m, ISNULL(SUM(y.Total), 0) AS [Invoiced], ISNULL(SUM(FEI.Total), 0) AS [Paid] FROM D x
  LEFT JOIN Invoices y ON CONVERT(VARCHAR, y.InvoiceDate, 102) = x.d_y_m
  LEFT JOIN Payments AS FEI ON CONVERT(VARCHAR, FEI.PaymentDate, 102) = x.d_y_m
  GROUP BY x.d_y_m
  ORDER BY x.d_y_m OPTION (MAXRECURSION 0)
  </code></pre>
  <p>How do I add another column (RunningTotal) to the query which sums up the (Invoiced-Paid) result from the previous day to the one for today</p>
  <p>Example:</p>
  <pre><code>d_y_m | Invoiced | Paid | RunningTotal
  
  2020.11.01 | 24 | 5 | 19
  
  2020.11.02 | 45 | 2 | 62
  
  2020.11.03 | 10 | 20 | 52
  
  2020.11.04 | 5 | 0 | 57
  
  2020.11.05 | 0 | 10 | 47
  </code></pre>