SQLTeam.com | Weblogs | Forums

Question


#1


#2

Please post DDL to create the table(s) and DML to insert row(s).
Thanks


#3

To do this - you want to add a row number that defines the order of the rows. Once you have the row number - you then join back to that on the row number - 1.

WITH rows
AS (
SELECT *
     , ROW_NUMBER() OVER(PARTITION BY Lot ORDER BY Date) As RowNum
   FROM YourTable
)
SELECT *, PreviousBalance = COALESCE(b.QtyBalance, 0)
FROM rows a
LEFT JOIN rows b ON b.RowNum = a.RowNum - 1

Should get you started...