-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaster_TSQL_Fifo.sql
67 lines (61 loc) · 1.72 KB
/
Master_TSQL_Fifo.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- Articles => http://www.kodyaz.com/t-sql/fifo-example-query-in-sql-server.aspx
-- link => http://sqlfiddle.com/#!6/7b7f6/1
-- DROP TABLE SalesOrder
-- DROP TABLE ProductionOrder
CREATE TABLE SalesOrder (
OrderId varchar(10),
OrderDate datetime,
ProductId varchar(10),
OrderQty int
);
GO
CREATE TABLE ProductionOrder (
OrderId varchar(10),
OrderDate datetime,
ProductId varchar(10),
OrderQty int
);
GO
INSERT INTO SalesOrder
SELECT 'SO-0001', '20120105 13:45', 'PROD-01', 50
INSERT INTO SalesOrder
SELECT 'SO-0002', '20120108 12:00', 'PROD-02', 40
INSERT INTO SalesOrder
SELECT 'SO-0003', '20120109 10:30', 'PROD-01', 20
INSERT INTO SalesOrder
SELECT 'SO-0004', '20120110 17:10', 'PROD-03', 30
INSERT INTO ProductionOrder
SELECT 'PO-0001', '20120115 15:00', 'PROD-01', 30
INSERT INTO ProductionOrder
SELECT 'PO-0002', '20120115 18:00', 'PROD-02', 20
INSERT INTO ProductionOrder
SELECT 'PO-0003', '20120116 18:00', 'PROD-01', 30
SELECT * FROM SalesOrder --order by ProductId
SELECT * FROM ProductionOrder --order by ProductId
---
;
WITH s AS
(SELECT *,
SoldUpToNow =
(SELECT sum(OrderQty)
FROM SalesOrder
WHERE ProductId = s.ProductId
AND OrderDate <= s.OrderDate)
FROM SalesOrder s),
p AS
(SELECT ProductId,
sum(OrderQty) AS TotalProduced
FROM ProductionOrder
GROUP BY ProductId)
SELECT *
FROM
(SELECT s.*,
p.TotalProduced,
CASE
WHEN s.SoldUpToNow - isnull(p.TotalProduced,0) < 0 THEN 0
WHEN (s.SoldUpToNow - isnull(p.TotalProduced,0)) > s.OrderQty THEN s.OrderQty
ELSE s.SoldUpToNow - isnull(p.TotalProduced,0)
END AS LeftQty
FROM s
LEFT JOIN p ON s.ProductId = p.ProductId) fifo
WHERE LeftQty > 0