Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
DECLARE @PREVIOUS_QTY int
-- Get previous QTY for the item, if there is one
SELECT TOP 1 @PREVIOUS_QTY = RUNNING_QTY
FROM <table>
WHERE ITEM=inserted.ITEM and
DATE<@inserted.DATE
-- If there is a previous qty for item then update
-- the running qty for the newly inserted, updated item
IF @PREV_QTY IS NOT NULL
BEGIN
UPDATE inserted
SET insert.RUNNING_QTY = @PREVIOUS_QTY + inserted.QTY
END
UPDATE M
SET RunningQty = Qty + IsNull(SELECT TOP 1 RunningQty FROM MainTable P WHERE P.Date < M.Date ORDER BY P.Date DESC)
FROM MainTable M
INNER JOIN Inserted I ON M.Date = I.Date
WHERE RunningQty IS NULL
UPDATE MainTable
SET
RunningQty = (SELECT Sum(Qty) FROM MainTable P WHERE P.Date < M.Date)
WHERE
RunningQty IS NULL