mdProgrammer
Programmer
I have a query that I'm working on which has the requirement that it calculates a netloss carryover (it's an accounting app, so accounting algorithms aren't my strong suit). The current netloss carryover field should add the income and netloss from the previous month. I found a running total example online using OVER PARTITION. This works fine for a running total, but there's a twist that I'm trying to figure out how to do - (Note - I'm using SQL Server 2016)
1 - When the "Net Loss Carryover" is >= 0, then show 0 and it resets the total.
2 - Sometimes, another condition may be met that resets the value to 0 (not included in the query, but let's assume it's some field flag for that particular row. I've seen reset code for this elsewhere).
Below, I have the output, and what I would like it to show.
[pre]
CREATE TABLE #Purchases
(
GroupID int,
TransactionDate date,
Price int
)
INSERT INTO #Purchases VALUES (1,'1/1/2017',80)
INSERT INTO #Purchases VALUES (1,'2/1/2017',20)
INSERT INTO #Purchases VALUES (1,'3/1/2017',-50)
INSERT INTO #Purchases VALUES (1,'4/1/2017',30)
INSERT INTO #Purchases VALUES (1,'5/1/2017',40)
INSERT INTO #Purchases VALUES (1,'6/1/2017',-100)
INSERT INTO #Purchases VALUES (1,'7/1/2017',-80)
INSERT INTO #Purchases VALUES (1,'8/1/2017',-20)
INSERT INTO #Purchases VALUES (1,'9/1/2017',10)
INSERT INTO #Purchases VALUES (1,'10/1/2017',25)
INSERT INTO #Purchases VALUES (1,'11/1/2017',15)
INSERT INTO #Purchases VALUES (1,'12/1/2017',50)
INSERT INTO #Purchases VALUES (1,'1/1/2018',30)
SELECT
GroupID,
TransactionDate,
Price,
SUM(Price) OVER (PARTITION BY GroupID ORDER BY TransactionDate) AS RunningTotal
FROM
#Purchases
DROP TABLE #Purchases
[/pre]
[pre]
GroupID TransactionDate Income RunningTotal Should Show (for Net Loss Carryover)
1 1/1/2017 80 80 0
1 2/1/2017 20 100 0
1 3/1/2017 -50 50 0
1 4/1/2017 30 80 -50
1 5/1/2017 40 120 -20
1 6/1/2017 -100 20 0
1 7/1/2017 -80 -60 -100
1 8/1/2017 -20 -80 -180
1 9/1/2017 65 -70 -200
1 10/1/2017 25 -45 -135
1 11/1/2017 30 -30 -110
1 12/1/2017 80 20 -80
1 1/1/2018 130 50 0
[/pre]
1 - When the "Net Loss Carryover" is >= 0, then show 0 and it resets the total.
2 - Sometimes, another condition may be met that resets the value to 0 (not included in the query, but let's assume it's some field flag for that particular row. I've seen reset code for this elsewhere).
Below, I have the output, and what I would like it to show.
[pre]
CREATE TABLE #Purchases
(
GroupID int,
TransactionDate date,
Price int
)
INSERT INTO #Purchases VALUES (1,'1/1/2017',80)
INSERT INTO #Purchases VALUES (1,'2/1/2017',20)
INSERT INTO #Purchases VALUES (1,'3/1/2017',-50)
INSERT INTO #Purchases VALUES (1,'4/1/2017',30)
INSERT INTO #Purchases VALUES (1,'5/1/2017',40)
INSERT INTO #Purchases VALUES (1,'6/1/2017',-100)
INSERT INTO #Purchases VALUES (1,'7/1/2017',-80)
INSERT INTO #Purchases VALUES (1,'8/1/2017',-20)
INSERT INTO #Purchases VALUES (1,'9/1/2017',10)
INSERT INTO #Purchases VALUES (1,'10/1/2017',25)
INSERT INTO #Purchases VALUES (1,'11/1/2017',15)
INSERT INTO #Purchases VALUES (1,'12/1/2017',50)
INSERT INTO #Purchases VALUES (1,'1/1/2018',30)
SELECT
GroupID,
TransactionDate,
Price,
SUM(Price) OVER (PARTITION BY GroupID ORDER BY TransactionDate) AS RunningTotal
FROM
#Purchases
DROP TABLE #Purchases
[/pre]
[pre]
GroupID TransactionDate Income RunningTotal Should Show (for Net Loss Carryover)
1 1/1/2017 80 80 0
1 2/1/2017 20 100 0
1 3/1/2017 -50 50 0
1 4/1/2017 30 80 -50
1 5/1/2017 40 120 -20
1 6/1/2017 -100 20 0
1 7/1/2017 -80 -60 -100
1 8/1/2017 -20 -80 -180
1 9/1/2017 65 -70 -200
1 10/1/2017 25 -45 -135
1 11/1/2017 30 -30 -110
1 12/1/2017 80 20 -80
1 1/1/2018 130 50 0
[/pre]