【发布时间】:2019-10-03 18:00:14
【问题描述】:
尝试根据表中的值计算新字段。计算出第一个字段后,需要在后面的行引用前一个值才能开始下一个计算。
已尝试使用滞后/领先功能,因为我需要进行分区。
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
我的预期结果将是一个使用此数学的新列。
T.[Current Backlog] - T.[Worked Hours] + T.[Sold Hours] = 'New Backlog'(对于第一行,这将是 190)。
随后的行将使用“新积压值”(190) 代替上一个等式中的 T.[Current Backlog]。
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
【问题讨论】: