我已经接管了一个项目,而对于我的实际情况,我不知道为什么这段代码在这里。我理解这个观点,好的,但不是这样的,因为它会导致查询在某一点上失败。
SELECT pp.productionplanid,
pp.weekstartdate,
pp.weekenddate,
ppi.productionplanitemid,
ppi.soporderreturnlineid,
bat.productionplanbatchid,
sop.documentno,
cust.customeraccountname,
bat.NAME AS BatchName,
si.itemid,
si.code AS StockCode,
si.NAME AS StockName,
sopLine.allocatedquantity,
o.quantity AS LineQty
FROM fuel_productionplan pp
LEFT JOIN fuel_productionplanitem ppi
ON pp.productionplanid = ppi.productionplanid
LEFT JOIN fuel_productionplanbatch ppb
ON ppi.productionplanbatchid = ppb.productionplanbatchid
LEFT JOIN soporderreturnline sopLine
ON ppi.soporderreturnlineid = sopLine.soporderreturnlineid
LEFT JOIN soporderreturn sop
ON sopLine.soporderreturnid = sop.soporderreturnid
LEFT JOIN slcustomeraccount cust
ON sop.customerid = cust.slcustomeraccountid
LEFT JOIN stockitem si
ON sopLine.itemcode = si.code
LEFT JOIN fuel_productionplanbatch bat
ON ppi.productionplanbatchid = bat.productionplanbatchid
LEFT JOIN fuel_boxed boxed
ON sopLine.soporderreturnlineid = boxed.sopitemlineid
AND pp.productionplanid = boxed.productionplanid
CROSS apply (SELECT 1
FROM master..spt_values v
WHERE v.type = 'P'
AND v.number < ( sopLine.allocatedquantity -
Isnull(boxed.qtyboxed, 0) )
) o(quantity)
WHERE sopLine.allocatedquantity > 0 这里的这个部分失败了,但由于某种原因,它影响了我的数据,我从未见过在master..spt_values之前使用以下内容。
CROSS apply
(
SELECT 1
FROM master..spt_values v
WHERE v.type = 'P'
AND v.number < (sopline.allocatedquantity - isnull(boxed.qtyboxed, 0))
) o(quantity)有人能向我解释一下,spt_values是什么,server中的一个函数,以及如何更好地删除交叉应用,我发现了有问题的项目,但是我担心V.number在做什么,为什么最初的程序员会使用它呢?
正如建议的那样,我运行子查询来查看它产生了什么,它产生了以下结果,但是我不明白它为什么会使我的数据返回。

编辑2
我相信我要做的就是得到这个结果
(sopLine.AllocatedQuantity - ISNULL(boxed.QtyBoxed, 0))) o(Quantity) 由于列LineQty的存在,在没有这种复杂性的情况下,没有一种更简单的方法来完成这个任务。
编辑3
当我删除交叉应用,只显示这两列,谁可以,然后请告诉我,交叉连接是如何制动数据被返回。

编辑4
当我试图在交叉连接的替换中做以下操作时,它就不起作用了。
sopLine.AllocatedQuantity - boxed.QtyBoxed AS LineQty编辑5
这是我的替换查询,它似乎工作,但它是否安全,是检查null足够好。
SELECT pp.ProductionPlanID, pp.WeekStartDate, pp.WeekEndDate, ppi.ProductionPlanItemID, ppi.SOPOrderReturnLineID, bat.ProductionPlanBatchID, sop.DocumentNo,
cust.CustomerAccountName, bat.Name AS BatchName, si.ItemID, si.Code AS StockCode, si.Name AS StockName, sopLine.AllocatedQuantity, boxed.QtyBoxed,
ISNULL(sopLine.AllocatedQuantity - boxed.QtyBoxed, 0) AS LineQty
FROM dbo.FUEL_ProductionPlan AS pp LEFT OUTER JOIN
dbo.FUEL_ProductionPlanItem AS ppi ON pp.ProductionPlanID = ppi.ProductionPlanID LEFT OUTER JOIN
dbo.FUEL_ProductionPlanBatch AS ppb ON ppi.ProductionPlanBatchID = ppb.ProductionPlanBatchID LEFT OUTER JOIN
dbo.SOPOrderReturnLine AS sopLine ON ppi.SOPOrderReturnLineID = sopLine.SOPOrderReturnLineID LEFT OUTER JOIN
dbo.SOPOrderReturn AS sop ON sopLine.SOPOrderReturnID = sop.SOPOrderReturnID LEFT OUTER JOIN
dbo.SLCustomerAccount AS cust ON sop.CustomerID = cust.SLCustomerAccountID LEFT OUTER JOIN
dbo.StockItem AS si ON sopLine.ItemCode = si.Code LEFT OUTER JOIN
dbo.FUEL_ProductionPlanBatch AS bat ON ppi.ProductionPlanBatchID = bat.ProductionPlanBatchID LEFT OUTER JOIN
dbo.FUEL_Boxed AS boxed ON sopLine.SOPOrderReturnLineID = boxed.SopItemLineID AND pp.ProductionPlanID = boxed.ProductionPlanID
WHERE (sopLine.AllocatedQuantity > 0)尤其是从上面的这条线。
ISNULL(sopLine.AllocatedQuantity - boxed.QtyBoxed, 0) AS LineQty发布于 2018-10-05 12:39:45
我和Dan都提到了一张理货表。在最简单的情况下(让我们称之为Numbers),它只包含一个列(让我们称它为number)。该表的每一行包含一个不同的整数值。在这种情况下,将使用来自无文档表master..spt_values的行子集。
因此,创建这样一个理货表,查询(如果逻辑正确)现在变成:
CROSS APPLY
(SELECT 1 FROM Numbers
WHERE
Numbers.number < (sopLine.AllocatedQuantity - ISNULL(boxed.QtyBoxed, 0))
) o(Quantity)(这假设您的理货表从0开始。如果它从1开始,使用<=而不是<。如果它也包含负数,也添加Numbers.number >=0作为附加条件)
正如我在下面的一个注释中所说的,这里的逻辑似乎是,如果(sopLine.AllocatedQuantity - ISNULL(boxed.QtyBoxed, 0))是6,我们希望cross apply在结果集中生成6行,并将它们的Quantity值设置为1。
https://stackoverflow.com/questions/52665312
复制相似问题