Michael
Michael

Reputation: 2657

T SQL Cumulative Subtraction

I'm using MS SQL Server.

I have the below table:

SKU     Shop    WeekNum    ShopPrioirty    Replen   OpeningStock
111     100         1            1           10          5000
111     200         1            2           10          NULL  
111     300         1            3           5           NULL
111     400         1            4           8           NULL

222     100         2            1           20          6000
222     200         2            2           15          NULL
222     300         2            3           12          NULL
222     400         2            4           10          NULL

This is the desired result:

SKU     Shop    WeekNum    ShopPrioirty    Replen   OpeningStock
111     100         1            1           10          5000
111     200         1            2           10          4990  
111     300         1            3           5           4980
111     400         1            4           8           4975

222     100         2            1           20          6000
222     200         2            2           15          5980
222     300         2            3           12          5965
222     400         2            4           10          5953

For a given week, a SKU exists in multiple shops and is assigned a priority. At Priority 1 the opening stock is assigned.

However, I need to update the Opening Stock (where it is currently NULL) to equal the previous Opening Stock Minus the Previous Replen.

Before I attempt the update, I tried to just do a SELECT

SELECT SKU
      ,Shop
      ,WeekNum
      ,StorePriority
      ,Replen
      ,OpeningStock
      ,OpeningStock - Replen OVER (ORDER BY SKU,Shop,WeekNum ROWS UNBOUNDED PRECEDING) AS Opening
       FROM [table1] t

But I receive the error:Incorrect syntax near the keyword 'OVER'.

Is a running sum the correct way to go?

Would it be best to create a key made up from the SKU\Shop\WeekNum\Priority?

Thanks.

Upvotes: 2

Views: 2176

Answers (1)

Thom A
Thom A

Reputation: 96016

I've made a couple of assumptions here on your PARTITION BY and ORDER BY clauses, but this gets you the result you're after. As you only have a value for OpeningStock in the first row for a SKU, then I use FIRST_VALUE to get the First Value, and then take away all prior values of Replen:

WITH VTE AS(
    SELECT *
    FROM (VALUES(111,100,1,1,10,5000),
                (111,200,1,2,10,NULL),  
                (111,300,1,3,5 ,NULL),
                (111,400,1,4,8 ,NULL),
                (222,100,2,1,20,6000),
                (222,200,2,2,15,NULL),
                (222,300,2,3,12,NULL),
                (222,400,2,4,10,NULL))V(SKU,Shop,WeekNum,ShopPrioirty,Replen,OpeningStock))
SELECT V.SKU,
       V.Shop,
       V.WeekNum,
       V.ShopPrioirty,
       V.Replen,
       V.OpeningStock,
       FIRST_VALUE(V.OpeningStock) OVER (PARTITION BY V.SKU ORDER BY V.ShopPrioirty,V.WeekNum ROWS UNBOUNDED PRECEDING) - 
       ISNULL(SUM(V.Replen) OVER (PARTITION BY V.SKU ORDER BY V.ShopPrioirty,V.WeekNum ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0) AS CurrentStock
FROM VTE V;

Upvotes: 7

Related Questions