我在SQL Server 2014中有两个表。
第一张表是原材料的累积需求:
create table #Demand
(
ItemPN nvarchar(17), /* Part Number, for example "AA-ECN0018" */
[Period] nvarchar(7), /* Period in format YYYY-MM, for example "2020-01" */
Qty int /* Actual demand */
)
第二张表是采购订单清单:
create table #PurchaseOrders
(
OrderNumber int, /* P/O number */
POLine int, /* P/O line number */
ItemPN nvarchar(17), /* Part Number, for example "AA-ECN0018" */
CurrentDelDate nvarchar(10), /* Delivery date in format YYYY-MM-DD, for example "2020-01-18" */
OrderedQty int. /*Qty that has been ordered initially in the Purchase Order line */
DeliveredQty int, /* Qty in the Purchase Order line that has been already delivered */
BackOrderQty /* Qty that the supplier has to supply, actually it is OrderedQty - DeliveredQty */
)
我需要找出在期间(YYYY-MM)范围内现有采购订单行未涵盖的零件编号的需求。因此,采购部门需要加快这些P / N的采购订单。
我已经计算出每个时期的累计需求:
SUM([Qty]) OVER(PARTITION BY Item ORDER BY Item, [Period] ROWS UNBOUNDED PRECEDING) AS DemandPerPeriod
然后我在P / N和Period的#Demand
和#PurchaseOrders
表之间进行了左联接:
SELECT
d.ItemPN DemandItem
,d.[Period] DemandPeriod
,t.[OrderNumber]
,t.[POLine]
,t.ItemPN
,[OrderedQty] - [DeliveredQty] as ActualBackorder
,d.DemandPerPeriod
,SUM([OrderedQty] - [DeliveredQty]) OVER(PARTITION BY ItemPN ORDER BY ItemPN, CurrentDelDate ROWS UNBOUNDED PRECEDING) RunningPur
,d.DemandPerPeriod -
SUM([OrderedQty] - [DeliveredQty]) OVER(PARTITION BY ItemPN ORDER BY ItemPN, CurrentDelDate ROWS UNBOUNDED PRECEDING)
AS NotCoveredDemand
t.CurrentDelDate
FROM #PurchaseOrders t
LEFT JOIN (
SELECT
ItemPN
,[Period]
,QTY
,SUM([Qty]) OVER(PARTITION BY Item ORDER BY Item, [Period] ROWS UNBOUNDED PRECEDING) DemandPerPeriod
FROM #Demand ) d ON d.ItemPN = t.ItemPN
AND d.Period = CONVERT(NVARCHAR(4), YEAR(CurrentDelDate)) + '-' + RIGHT('00' + CONVERT(NVARCHAR(2), Month(CurrentDelDate)), 2)
WHERE ROUND(BackorderQty, 0) > 0
ORDER BY ItemPN, Period
我真正需要的是:
我在SQL Server 2014中有两个表。第一个表是原材料的累积需求:创建表#Demand(ItemPN nvarchar(17),/ *零件号,例如“ AA-ECN0018” * / [。 ..
也许是这样:
WITH Demands
AS
(
SELECT d.ItemPN, [Period], SUM(Qty) AS TotalDemand
FROM #Demand AS d
GROUP BY d.ItemPN, [Period]
),
ExistingOrders
AS
(
SELECT po.ItemPN, LEFT(po.CurrentDelDate, 7) AS [Period],
SUM(po.OrderedQty - po.DeliveredQty) AS TotalBackorders
FROM #PurchaseOrders AS po
ON d.ItemPN = po.ItemPN
AND d.[Period] = LEFT(po.CurrentDelDate, 7)
GROUP BY po.ItemPN, LEFT(po.CurrentDelDate, 7)
),
UndersuppliedDemand
AS
(
SELECT d.ItemPN,
d.[Period],
d.TotalDemand - COALESCE(eo.TotalBackorders) AS TotalUndersupplied
FROM Demands AS d
LEFT OUTER JOIN ExistingOrders AS eo
ON d.ItemPN = eo.ItemPN
AND d.[Period] = eo.[Period]
WHERE d.TotalDemand > eo.TotalBackorders
)
SELECT ud.ItemPN,
ud.[Period],
o.OrderNumber,
o.POLine
FROM UndersuppliedDemand AS ud
CROSS APPLY
(
SELECT po.OrderNumber,
po.POLine
FROM #PurchaseOrders AS po
WHERE po.ItemPN = ud.ItemPN
AND LEFT(po.CurrentDelDate, 7) = ud.[Period]
AND po.OrderNumber = (SELECT MAX(pom.OrderNumber)
FROM #PurchaseOrders AS pom
WHERE pom.ItemPN = ud.ItemPN
AND LEFT(pom.CurrentDelDate, 7) = ud.[Period])
) AS o;