I am maintaining an inventory in azure sql DW and want to calculate initial and final stock of a day. Final stock of previous day will be initial stock for present day and Final stock for present day will be initial stock+Produced-sold. I have details of Produced and Sold quantities. How can I calculate initial and final quantities.
I am able to use lag function, but cant add the calculated value to get the finalqunatity in same query.
The query i wany to write but doestnt work is
Select lag(finalquantity,1) over ( Partition by Productid Order by Date) as Initialqty, (Initialqty+Produced-sold) as finalquantity from table
The finalquantity should come as a initialquantity for next record. I want this to happen recursively.