Attempting to calculate a new field based off of values in table. Once First field is calculated need following rows the reference the previous value to begin next calculation.
Have attempted using Lag/Lead Functions as I will need to partition.
declare @test as table (
[Worked Hours] decimal(2,0),
[Sold Hours] decimal(3,0),
[Current Backlog] decimal(3,0),
[Product] nvarchar(10),
[Revenue Type] nvarchar(3),
[Month] date
)
INSERT INTO @TEST
([Worked Hours], [Sold Hours], [Current Backlog], [Product], [Revenue Type], [Month])
VALUES
('10','150','50', 'Product', 'Revenue', '01-01-2019'),
('25','200','50', 'Product', 'Revenue', '02-01-2019'),
('15','175','50', 'Product', 'Revenue', '03-01-2019'),
('40','250','50', 'Product', 'Revenue', '04-01-2019')
select
t.[Product],
t.[Revenue Type],
t.[Month],
t.[Worked Hours],
t.[Sold Hours],
t.[Current Backlog]
from @test as T
My expected Result would be a new column that would Use this Math.
T.[Current Backlog] - T.[Worked Hours] + T.[Sold Hours] = 'New Backlog' (For the First Line this would be 190).
The subsequent line would use the "New Backlog Value" (190) as substitution for T.[Current Backlog] in previous equation.
Overall Expected Results
Product Revenue 2019-01-01 10 150 50 190
Product Revenue 2019-02-01 25 200 50 365
Product Revenue 2019-03-01 15 175 50 525
Product Revenue 2019-04-01 40 250 50 735