例如如果为运行总计求和的值有时为负数,则运行总计可能会下降。但对于每一行,我还希望达到该点的最大运行总数...
如何对此代码进行增强?
select ItemId, DateId, TargetRunningStock,
sum(TargetRunningStock) over (partition by itemid order by dateid)
from WarehouseData
您可以添加另一个级别:
select ItemId, DateId, TargetRunningStock, RunTot,
Max(RunTot) over (partition by itemid order by dateid) as MaxRunTot
from (
select ItemId, DateId, TargetRunningStock,
sum(TargetRunningStock) over (partition by itemid order by dateid) as RunTot
from WarehouseData
) Step1