I have a table which looks like this:
&ItemNo
&LocationId
&MovementDate
MovementWithinDay
Stock -- the field I need to update with sum of MovementWithinDay for all older records for same item
As this table contains over 300 million rows it takes very long time to populate the Stock field. Each row contains the movements within that day and what I need to do is to calculate the stock based on the sum of MovementWithinDay for all records for this item that are older than this record. This is a one-time-transaction so moving into another structure is an option. I have used cursor to loop through the records but it was incredibly slow. Query with summing up all older records for that item for each record was consideribly faster but very slow.
Could anybody give me pointers to do this in the least amount of time?
So, from how I’ve understood your question, you want running sums of
MovementWithinDayfor every&ItemNo. I believe you could employ a recursive CTE with ranking for that, unless you are on SQL Server 2000 or earlier version.This is how the query might look: