RunningSUM
Updated: 31 Oct 2012
Use RunningSUM to calculate the sum of column values in an ordered resultant table, without the need for a self-join. The sum is calculated for each value from the first value to the last value in the ordered group or partition. If the column values are presented to the functions out of order, an error message will be generated.
Syntax
SELECT [Example].[wct].[RunningSUM](
<@Val, float,>
,<@Round, int,>
,<@RowNum, int,>
,<@Id, tinyint,>)
Arguments
@Val
the value passed into the function. @Val is an expression of type float or of a type that can be implicitly converted to float.
@Round
the number of decimals places to store the result. @Round is an expression of type int or of a type that can be implicitly converted to int.
@RowNum
the number of the row within the group for which the sum is being calculated. If @RowNum for the current row in a set is less than or equal to the previous @RowNum and @RowNum is not equal to 1, an error message will be generated. @RowNum is an expression of type int or of a type that can be implicitly converted to int.
@Id
a unique identifier for the RunningSUM calculation. @Id allows you to specify multiple RunningSUM calculations within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.
Remarks
· If @Id is NULL then @Id = 0.
· To calculate moving sums, use the MovingSUM function.
· If @RowNum is equal to 1, RunningSUM is equal to @Val
· @RowNum must be in ascending order.
· There may be cases where the order in which the data are returned to the function and the order in which the results are returned are different, generally due to parallelism. You can use OPTION(MAXDOP 1) or OPTION(MAXDOP 1,FORCE ORDER) to help eliminate this problem.
Example
In this example we will calculate the running balance for a check book. We will create a temporary table, #c, populate it with some data and then run the SELECT.
--Create the temporary table
CREATE TABLE #c (
trn int,
cust_ref varchar(5),
date_trn datetime,
payee varchar(20),
amt_trn money,
PRIMARY KEY (trn)
)
--Populate the table with some data
INSERT INTO #c VALUES (1,'DD','2011-12-23',NULL,1500)
INSERT INTO #c VALUES (2,'1200','2012-01-01','Car Leasing Company',-361.75)
INSERT INTO #c VALUES (3,'1201','2012-01-01','Mortgage Bank',-1129.23)
INSERT INTO #c VALUES (4,'DD','2012-01-08',NULL,1100)
INSERT INTO #c VALUES (5,'1202','2012-01-15','Gas Company',-108.49)
INSERT INTO #c VALUES (6,'1203','2012-01-15','Electric Company',-98.27)
INSERT INTO #c VALUES (7,'1204','2012-01-15','Telephone Company',-136.6)
INSERT INTO #c VALUES (8,'DD','2012-02-15',NULL,1100)
INSERT INTO #c VALUES (9,'1205','2012-02-01','Car Leasing Company',-361.75)
INSERT INTO #c VALUES (10,'1206','2012-02-01','Mortgage Bank',-1129.23)
INSERT INTO #c VALUES (11,'DD','2012-02-08',NULL,1100)
INSERT INTO #c VALUES (12,'1207','2012-02-15','Gas Company',-153.89)
INSERT INTO #c VALUES (13,'1208','2012-02-15','Electric Company',-121.99)
INSERT INTO #c VALUES (14,'1209','2012-02-15','Telephone Company',-138.92)
INSERT INTO #c VALUES (15,'DD','2012-02-23',NULL,1100)
INSERT INTO #c VALUES (16,'1210','2012-03-01','Car Leasing Company',-361.75)
INSERT INTO #c VALUES (17,'1211','2012-03-01','Mortgage Bank',-1129.23)
INSERT INTO #c VALUES (18,'DD','2012-03-08',NULL,1100)
INSERT INTO #c VALUES (19,'1212','2012-03-15','Gas Company',-70.79)
INSERT INTO #c VALUES (20,'1213','2012-03-15','Electric Company',-93.57)
INSERT INTO #c VALUES (21,'1214','2012-03-15','Telephone Company',-149.78)
INSERT INTO #c VALUES (22,'DD','2012-03-23',NULL,1100)
INSERT INTO #c VALUES (23,'1215','2012-04-01','Car Leasing Company',-361.75)
INSERT INTO #c VALUES (24,'1216','2012-04-01','Mortgage Bank',-1129.23)
INSERT INTO #c VALUES (25,'DD','2012-04-08',NULL,1100)
INSERT INTO #c VALUES (26,'1217','2012-04-15','Gas Company',-105.58)
INSERT INTO #c VALUES (27,'1218','2012-04-15','Electric Company',-149.36)
INSERT INTO #c VALUES (28,'1219','2012-04-15','Telephone Company',-145.35)
INSERT INTO #c VALUES (29,'DD','2012-04-23',NULL,1100)
INSERT INTO #c VALUES (30,'1220','2012-05-01','Car Leasing Company',-361.75)
INSERT INTO #c VALUES (31,'1221','2012-05-01','Mortgage Bank',-1129.23)
INSERT INTO #c VALUES (32,'DD','2012-05-08',NULL,1100)
INSERT INTO #c VALUES (33,'1222','2012-05-15','Gas Company',-96.27)
INSERT INTO #c VALUES (34,'1223','2012-05-15','Electric Company',-114.62)
INSERT INTO #c VALUES (35,'1224','2012-05-15','Telephone Company',-145.43)
INSERT INTO #c VALUES (36,'DD','2012-05-23',NULL,1100)
INSERT INTO #c VALUES (37,'1225','2012-06-01','Car Leasing Company',-361.75)
INSERT INTO #c VALUES (38,'1226','2012-06-01','Mortgage Bank',-1129.23)
INSERT INTO #c VALUES (39,'DD','2012-06-08',NULL,1100)
INSERT INTO #c VALUES (40,'1227','2012-06-15','Gas Company',-147.03)
INSERT INTO #c VALUES (41,'1228','2012-06-15','Electric Company',-130.52)
INSERT INTO #c VALUES (42,'1229','2012-06-15','Telephone Company',-147.71)
INSERT INTO #c VALUES (43,'DD','2012-06-23',NULL,1100)
INSERT INTO #c VALUES (44,'1230','2012-07-01','Car Leasing Company',-361.75)
INSERT INTO #c VALUES (45,'1231','2012-07-01','Mortgage Bank',-1129.23)
INSERT INTO #c VALUES (46,'DD','2012-07-08',NULL,1100)
--Calculate the running total
SELECT cast(convert(varchar, date_trn, 106) as char(11)) as date_trn
,cust_ref
,payee
,amt_trn
,wct.RunningSUM(amt_trn,2,trn,NULL) as [Running Total]
FROM #c
--Clean up
DROP TABLE #c
This produces the following result.
date_trn cust_ref payee amt_trn Running Total
----------- -------- -------------------- --------------------- ----------------------
23 Dec 2011 DD NULL 1500.00 1500
01 Jan 2012 1200 Car Leasing Company -361.75 1138.25
01 Jan 2012 1201 Mortgage Bank -1129.23 9.02
08 Jan 2012 DD NULL 1100.00 1109.02
15 Jan 2012 1202 Gas Company -108.49 1000.53
15 Jan 2012 1203 Electric Company -98.27 902.26
15 Jan 2012 1204 Telephone Company -136.60 765.66
15 Feb 2012 DD NULL 1100.00 1865.66
01 Feb 2012 1205 Car Leasing Company -361.75 1503.91
01 Feb 2012 1206 Mortgage Bank -1129.23 374.68
08 Feb 2012 DD NULL 1100.00 1474.68
15 Feb 2012 1207 Gas Company -153.89 1320.79
15 Feb 2012 1208 Electric Company -121.99 1198.8
15 Feb 2012 1209 Telephone Company -138.92 1059.88
23 Feb 2012 DD NULL 1100.00 2159.88
01 Mar 2012 1210 Car Leasing Company -361.75 1798.13
01 Mar 2012 1211 Mortgage Bank -1129.23 668.9
08 Mar 2012 DD NULL 1100.00 1768.9
15 Mar 2012 1212 Gas Company -70.79 1698.11
15 Mar 2012 1213 Electric Company -93.57 1604.54
15 Mar 2012 1214 Telephone Company -149.78 1454.76
23 Mar 2012 DD NULL 1100.00 2554.76
01 Apr 2012 1215 Car Leasing Company -361.75 2193.01
01 Apr 2012 1216 Mortgage Bank -1129.23 1063.78
08 Apr 2012 DD NULL 1100.00 2163.78
15 Apr 2012 1217 Gas Company -105.58 2058.2
15 Apr 2012 1218 Electric Company -149.36 1908.84
15 Apr 2012 1219 Telephone Company -145.35 1763.49
23 Apr 2012 DD NULL 1100.00 2863.49
01 May 2012 1220 Car Leasing Company -361.75 2501.74
01 May 2012 1221 Mortgage Bank -1129.23 1372.51
08 May 2012 DD NULL 1100.00 2472.51
15 May 2012 1222 Gas Company -96.27 2376.24
15 May 2012 1223 Electric Company -114.62 2261.62
15 May 2012 1224 Telephone Company -145.43 2116.19
23 May 2012 DD NULL 1100.00 3216.19
01 Jun 2012 1225 Car Leasing Company -361.75 2854.44
01 Jun 2012 1226 Mortgage Bank -1129.23 1725.21
08 Jun 2012 DD NULL 1100.00 2825.21
15 Jun 2012 1227 Gas Company -147.03 2678.18
15 Jun 2012 1228 Electric Company -130.52 2547.66
15 Jun 2012 1229 Telephone Company -147.71 2399.95
23 Jun 2012 DD NULL 1100.00 3499.95
01 Jul 2012 1230 Car Leasing Company -361.75 3138.2
01 Jul 2012 1231 Mortgage Bank -1129.23 2008.97
08 Jul 2012 DD NULL 1100.00 3108.97
In this example, we calculate the running total and the running total for each month and use the ROW_NUMBER() function to determine the @RowNum value passed into the RunningSUM function.
SELECT cast(convert(varchar, date_trn, 106) as char(11)) as date_trn
,cust_ref
,payee
,amt_trn
,wct.RunningSUM(amt_trn,2,ROW_NUMBER() OVER (PARTITION by MONTH(date_trn) ORDER BY trn),1) as [MTD Total]
,wct.RunningSUM(amt_trn,2,trn,NULL) as [YTD Total]
FROM #c
ORDER BY trn
This produces the following result.
date_trn cust_ref payee amt_trn MTD Total YTD Total
----------- -------- -------------------- --------- --------- ---------
23 Dec 2011 DD NULL 1500.00 1500.00 1500.00
01 Jan 2012 1200 Car Leasing Company -361.75 -361.75 1138.25
01 Jan 2012 1201 Mortgage Bank -1129.23 -1490.98 9.02
08 Jan 2012 DD NULL 1100.00 -390.98 1109.02
15 Jan 2012 1202 Gas Company -108.49 -499.47 1000.53
15 Jan 2012 1203 Electric Company -98.27 -597.74 902.26
15 Jan 2012 1204 Telephone Company -136.60 -734.34 765.66
15 Feb 2012 DD NULL 1100.00 1100.00 1865.66
01 Feb 2012 1205 Car Leasing Company -361.75 738.25 1503.91
01 Feb 2012 1206 Mortgage Bank -1129.23 -390.98 374.68
08 Feb 2012 DD NULL 1100.00 709.02 1474.68
15 Feb 2012 1207 Gas Company -153.89 555.13 1320.79
15 Feb 2012 1208 Electric Company -121.99 433.14 1198.80
15 Feb 2012 1209 Telephone Company -138.92 294.22 1059.88
23 Feb 2012 DD NULL 1100.00 1394.22 2159.88
01 Mar 2012 1210 Car Leasing Company -361.75 -361.75 1798.13
01 Mar 2012 1211 Mortgage Bank -1129.23 -1490.98 668.90
08 Mar 2012 DD NULL 1100.00 -390.98 1768.90
15 Mar 2012 1212 Gas Company -70.79 -461.77 1698.11
15 Mar 2012 1213 Electric Company -93.57 -555.34 1604.54
15 Mar 2012 1214 Telephone Company -149.78 -705.12 1454.76
23 Mar 2012 DD NULL 1100.00 394.88 2554.76
01 Apr 2012 1215 Car Leasing Company -361.75 -361.75 2193.01
01 Apr 2012 1216 Mortgage Bank -1129.23 -1490.98 1063.78
08 Apr 2012 DD NULL 1100.00 -390.98 2163.78
15 Apr 2012 1217 Gas Company -105.58 -496.56 2058.20
15 Apr 2012 1218 Electric Company -149.36 -645.92 1908.84
15 Apr 2012 1219 Telephone Company -145.35 -791.27 1763.49
23 Apr 2012 DD NULL 1100.00 308.73 2863.49
01 May 2012 1220 Car Leasing Company -361.75 -361.75 2501.74
01 May 2012 1221 Mortgage Bank -1129.23 -1490.98 1372.51
08 May 2012 DD NULL 1100.00 -390.98 2472.51
15 May 2012 1222 Gas Company -96.27 -487.25 2376.24
15 May 2012 1223 Electric Company -114.62 -601.87 2261.62
15 May 2012 1224 Telephone Company -145.43 -747.30 2116.19
23 May 2012 DD NULL 1100.00 352.70 3216.19
01 Jun 2012 1225 Car Leasing Company -361.75 -361.75 2854.44
01 Jun 2012 1226 Mortgage Bank -1129.23 -1490.98 1725.21
08 Jun 2012 DD NULL 1100.00 -390.98 2825.21
15 Jun 2012 1227 Gas Company -147.03 -538.01 2678.18
15 Jun 2012 1228 Electric Company -130.52 -668.53 2547.66
15 Jun 2012 1229 Telephone Company -147.71 -816.24 2399.95
23 Jun 2012 DD NULL 1100.00 283.76 3499.95
01 Jul 2012 1230 Car Leasing Company -361.75 -361.75 3138.20
01 Jul 2012 1231 Mortgage Bank -1129.23 -1490.98 2008.97
08 Jul 2012 DD NULL 1100.00 -390.98 3108.97