计算未售出产品的累计总和

时间:2016-02-25 10:15:16

标签: mysql cumulative-sum

我试图使用mysql显示未售出的产品总数,我想用FIFO方式计算它

例如

购买

stockdate   stockquantity 
24/02          100
25/02          200

销售

salesdate   salesquantity
25/02          50

输出

未售出物品= 250

但是物品应该从24/02开始销售

以下是我到目前为止尝试的程序

BEGIN

DECLARE item_sold INT;
DECLARE total_quantity INT;

SELECT item_sold= (SELECT sum(salesquantity) from Sales);

SELECT total_quantity = (SELECT stockquantity , case when stockquantity < item_sold then item_sold = item_sold-stockquantity when stockquantity > item_sold then stockquantity= stockquantity-item_sold AND item_sold = 0 AND +total_quantity when stockquantity = item_sold then item_sold = 0 end as total_quantity from Purchase order by stockdate);

select item_sold;
select total_quantity;
END

任何人都可以建议我做错了吗?或者这是正确的做法吗?

0 个答案:

没有答案