我有一个跟踪一组消耗资产的数据库,并将现有库存与下个月的订单预测进行比较。它通过对每个零件进行存款和取款的总和来处理现有库存(当使用零件或从供应商处收到新零件时,它们会被检入和检出)。
我的差距分析查询采用输入参数Month和Year,并返回现有和预测之间的差距。这适用于已经有过交易的库存零件,但尚未“初始化”到我的交易表中的零件将不会出现在差距分析中。
是否建议只编写一个追加查询,将每个部分的0-sum事务添加到事务表中?这看起来有点脏,但它可能有用,但我想知道是否有更优雅或最佳实践的方法来做到这一点。
这是我的qryGapAnalysis SQL;公平警告,我只是使用Access查询设计,所以它很恶心,但它可能会有所帮助。
SELECT tblInventory.electrodeID, tblJobs.ForecastYear,
tblJobs.ForecastMonth, tblJobs.jobNumber, tblJobs.Part, tblJobs.partQty,
tblInventory.partsPerElectrode, [Qty In]-[Qty Out] AS OnHand,
IIf(Round(([partQty]/[partsPerElectrode])-Sum([Qty In]-[Qty
Out]))>0,Round(([partQty]/[partsPerElectrode])-Sum([Qty In]-[Qty Out])),0)
AS Need
FROM ((tblParts INNER JOIN tblInventory ON tblParts.[Part #] = tblInventory.
[partID]) INNER JOIN tblJobs ON tblParts.[Part #] = tblJobs.Part) INNER JOIN
tblTransactions ON tblInventory.electrodeID = tblTransactions.Electrode
GROUP BY tblInventory.electrodeID, tblJobs.ForecastYear,
tblJobs.ForecastMonth, tblJobs.jobNumber, tblJobs.Part, tblJobs.partQty,
tblInventory.partsPerElectrode, [Qty In]-[Qty Out], tblInventory.electrodeID
HAVING (((tblJobs.ForecastYear)=Forms!frmGapAnalysisParameters!ComboYear)
And ((tblJobs.ForecastMonth)=Forms!frmGapAnalysisParameters!ComboMonth));
答案 0 :(得分:0)
尝试这样的事情:
select a.electrodeID, b.* from
(select b.* from tblParts a INNER JOIN tblInventory b ON tblParts.[Part #] = tblInventory)a
left join
(SELECT tblInventory.electrodeID, tblJobs.ForecastYear,
tblJobs.ForecastMonth, tblJobs.jobNumber, tblJobs.Part, tblJobs.partQty,
tblInventory.partsPerElectrode, [Qty In]-[Qty Out] AS OnHand,
IIf(Round(([partQty]/[partsPerElectrode])-Sum([Qty In]-[Qty
Out]))>0,Round(([partQty]/[partsPerElectrode])-Sum([Qty In]-[Qty Out])),0)
AS Need
FROM ((tblParts INNER JOIN tblInventory ON tblParts.[Part #] = tblInventory.
[partID]) INNER JOIN tblJobs ON tblParts.[Part #] = tblJobs.Part) INNER JOIN
tblTransactions ON tblInventory.electrodeID = tblTransactions.Electrode
GROUP BY tblInventory.electrodeID, tblJobs.ForecastYear,
tblJobs.ForecastMonth, tblJobs.jobNumber, tblJobs.Part, tblJobs.partQty,
tblInventory.partsPerElectrode, [Qty In]-[Qty Out], tblInventory.electrodeID
HAVING (((tblJobs.ForecastYear)=Forms!frmGapAnalysisParameters!ComboYear)
And ((tblJobs.ForecastMonth)=Forms!frmGapAnalysisParameters!ComboMonth))) b on a.electrodeID=b.electrodeID