clear markdown compare help donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 460387 distinct fiddles created so far.

CREATE TABLE TableA ([Date] datetime, [Units] int, [Remaining] int) ; INSERT INTO TableA ([Date], [Units], [Remaining]) VALUES ('2018-06-01 00:00:00', 2, 98), ('2018-06-02 00:00:00', 10, 88), ('2018-06-03 00:00:00', 15, 73), ('2018-06-04 00:00:00', 22, 51), ('2018-06-05 00:00:00', 5, 46), ('2018-06-06 00:00:00', 4, 42), ('2018-06-07 00:00:00', 3, 39), ('2018-06-08 00:00:00', 2, 37), ('2018-06-09 00:00:00', 1, 36), ('2018-06-10 00:00:00', 3, 33) ; SELECT [Date], [Units] ,100 - SUM(Units) OVER(ORDER BY Date ASC) AS [Remaining_Calculated] ,[Remaining] FROM TableA
Date Units Remaining_Calculated Remaining
01/06/2018 00:00:00 2 98 98
02/06/2018 00:00:00 10 88 88
03/06/2018 00:00:00 15 73 73
04/06/2018 00:00:00 22 51 51
05/06/2018 00:00:00 5 46 46
06/06/2018 00:00:00 4 42 42
07/06/2018 00:00:00 3 39 39
08/06/2018 00:00:00 2 37 37
09/06/2018 00:00:00 1 36 36
10/06/2018 00:00:00 3 33 33
 hidden batch(es)