sql 查询 fifo 库存

sql query for fifo inventory

我遇到了一个简单的 fifo sql 查询问题(计算每个销售日的利润)。

有两个表 Production 和 Invoice。对于每天的销售,我必须使用 FIFO 方法输出总销售利润。

例如,对于第二天的利润,我必须使用前一天的剩余物品及其尊重的价格。

这是表格和期望的输出结果

CREATE TABLE Production
(
    id int identity(1,1) primary key,
    Productid varchar(10),
    pdate date,
    Qty int,
    Price decimal(18, 2),
);

INSERT INTO Production (Productid,pDate, Qty ,Price) VALUES ('PD1', '01/01/2017', 8, 200);
INSERT INTO Production (Productid,pDate ,Qty ,Price) VALUES ('PD2', '02/01/2017', 14, 300);
INSERT INTO Production (Productid,pDate ,Qty ,Price) VALUES ('PD3', '03/01/2017', 15, 150);

CREATE TABLE Sales
(
    id int identity(1,1) primary key,
    Sid varchar(10),
    sDate date,
    Productid varchar(10),
    Qty int,
);

INSERT INTO Sales (Sid,sDate ,Productid ,Qty) VALUES ('S001', '04/01/2017', 'PD1', 5);
INSERT INTO Sales (Sid,sDate ,Productid ,Qty) VALUES ('S002', '05/01/2019', 'PD2', 4);
INSERT INTO Sales (Sid,sDate ,Productid ,Qty) VALUES ('S003', '06/01/2019', 'PD3', 6);

手动计算每天剩余的公式 (现有-销售数量)+采购数量=剩余

希望这会有所帮助。

SELECT 
     s.sid,
     s.sdate,
     p.productid,
     s.qty,
     CASE 
        WHEN s.qty <= p.qty 
            THEN s.qty*p.price 
        ELSE p.qty*p.price + (s.qty-p.qty) * (SELECT price FROM purchase WHERE pdate IN (SELECT MAX(pdate) FROM purchase WHERE pdate < s.sdate))
     END  AS PROFIT
 FROM purchase p
 JOIN sales s 
   ON p.productid = s.productid
      AND p.pdate = s.sdate

我认为对 sales.qty < purchase.qty 进行简单的检查是行不通的。因为即使你有 sales.qty < purchase.qty 但有最后一天的剩菜,那么你将首先使用这些剩菜。 你应该试试这个:

   with cte as(
select s.id,s.Sid,sDate,s.Productid,s.qty AS Qty,s.qty as saleqty,p.qty as productqty,p.price
  ,sum(p.qty-s.qty) over (order by sdate) as leftover
  from purchase P
inner join sales S
on p.productid=s.productid
and p.pdate=s.sdate
)
select id, Sid,sDate,Productid,Qty,
case when lag(leftover) over (order by sdate)>0 then lag(leftover *price) over( order by sdate)
+( saleqty-lag(leftover) over (order by sdate)) * price
else saleqty * price end as profit
from cte;