As you can see, the Menge
column is decreasing its value for every row, subtracting the Reserviert
value from the previous row.
Can anybody suggest how to write this type of query in SQL Server?
As you can see, the Menge
column is decreasing its value for every row, subtracting the Reserviert
value from the previous row.
Can anybody suggest how to write this type of query in SQL Server?
You can use window functions -- in particular a cumulative sum. It is not clear where the 200 is coming from, so I will hardcode it:
select t.*,
(200 -
sum(reservevert) over (partition by artikelnumber order by coalesce(lieferdatum, '2000-01-01')
) as merge
from t;
It Cannot be Done by SQL Only . It must to be done by Code