Handling negative values with sql

后端 未结 2 1178
花落未央
花落未央 2021-01-25 02:42

I have a data set that lists the date and quantity of future stock of products. Occasionally our demand outstrips our future supply and we wind up with a negative future quantit

2条回答
  •  再見小時候
    2021-01-25 02:56

    Here is a CROSS APPLY - tested

    SELECT b.ID,SKU,b.DATE,SEASON,QUANTITY
    FROM (
      SELECT SKU,SEASON, SUM(QUANTITY) AS QUANTITY
      FROM T1 
      GROUP BY SKU,SEASON
      ) a
    CROSS APPLY (
      SELECT TOP 1 b.ID,b.Date FROM T1 b
      WHERE a.SKU = b.SKU AND a.SEASON = b.SEASON
      ORDER BY b.ID ASC
      ) b
      ORDER BY ID ASC
    

提交回复
热议问题