我有一个数据集,列出了未来产品库存的日期和数量。有时,我们的需求超过了我们未来的供应,我们最终会得到一个负的未来数量。我需要将未来的负数量计入之前的供应量,这样我们就不会因超卖供应而加剧问题。
在以下数据集中,我需要为 10 至 19 日的需求做准备,将负数沿链向上应用,直到我得到一个正数:
"ID","SKU","DATE","SEASON","QUANTITY"
"1","001","2012-06-22","S12","1656"
"2","001","2012-07-13","F12","1986"
"3","001","2012-07-27","F12","-283"
"4","001","2012-08-17","F12","2718"
"5","001","2012-08-31","F12","-4019"
"6","001","2012-09-14","F12","7212"
"7","001","2012-09-21","F12","782"
"8","001","2012-09-28","F12","2073"
"9","001","2012-10-12","F12","1842"
"10","001","2012-10-19","F12","-12159"
我需要做到这一点:
"ID","SKU","DATE","SEASON","QUANTITY"
"1","001","2012-06-22","S12","1656"
"2","001","2012-07-13","F12","152"
我已经研究过使用 while 循环和外部应用,但似乎还没有找到一种方法来做到这一点。任何帮助将非常感激。这需要适用于 sql server 2008 R2。
这是另一个例子:
"1","002","2012-07-13","S12","1980"
"2","002","2012-08-10","F12","-306"
"3","002","2012-09-07","F12","826"
会成为:
"1","002","2012-07-13","S12","1674"
"3","002","2012-09-07","F12","826"