我有以下sql语句:
SELECT TOP (100) PERCENT inv.CompanyID,
cust.CustID AS ClientID,
cust.CustName AS CustomerName,
inv.InvcKey, inv.PrimarySperKey AS SperKey,
inv.TranID AS InvoiceNumber,
inv.TranDate AS PostDate,
sper.SperName AS SalesPersonName,
inv.SalesAmt AS InvoiceSubAmount,
inv.TranAmt AS InvoiceTotal,
detl.ItemKey,
detl.InvoiceLineKey AS dtInvoiceLineKey,
detl.Description,
detl.UnitCost AS calcUnitCost,
detl.UnitPrice,
detl.ExtAmt,
(detl.UnitPrice - detl.UnitCost) * dist.QtyShipped - detl.TradeDiscAmt AS detLineGrossProfit,
dbo.tPA00175.chrJobNumber AS ARJobNumber,
dist.QtyShipped,
dbo.timItem.ItemID AS ARItemID,
dbo.timItemClass.ItemClassID AS ARItemClass,
dist.TradeDiscAmt,
dbo._v_GP_SalesTerr.SalesTerritoryID
FROM
dbo.tarInvoiceDetl AS detl RIGHT OUTER JOIN
dbo.timItem INNER JOIN
dbo.timItemClass ON dbo.timItem.ItemClassKey = dbo.timItemClass.ItemClassKey ON detl.ItemKey = dbo.timItem.ItemKey RIGHT OUTER JOIN
dbo._v_GP_SalesTerr RIGHT OUTER JOIN
dbo.tarInvoice AS inv INNER JOIN
dbo.tarCustomer AS cust ON inv.CustKey = cust.CustKey ON dbo._v_GP_SalesTerr.CustKey = cust.CustKey ON
detl.InvcKey = inv.InvcKey LEFT OUTER JOIN
dbo.tPA00175 INNER JOIN
dbo.paarInvcHdr ON dbo.tPA00175.intJobKey = dbo.paarInvcHdr.intJobKey ON inv.InvcKey = dbo.paarInvcHdr.InvcKey LEFT OUTER JOIN
dbo.tarInvoiceLineDist AS dist ON detl.InvoiceLineKey = dist.InvoiceLineKey LEFT OUTER JOIN
dbo.tarSalesperson AS sper ON inv.PrimarySperKey = sper.SperKey
如果我想添加一个名为Inventory.LastUnitCost的字段,其中Inventory有一个名为itemkey的代理键,用于表“timitem”,我如何在那里添加它,只需按itemkey查找LastUnitCost字段?如果没有对应于timitem表的LastUnitCost,它将只为null ..
每当我尝试将它作为内部联接添加到Inventory.itemkey = timItem.itemkey上的timItem时,它会影响timItemClass.ItemClassID字段,其中一些catorgies不再显示。
提前感谢。
答案 0 :(得分:2)
将Inventory.LastUnitCost
添加到SELECT
列表
并将此行添加到查询的末尾:
dbo.Inventory LEFT OUTER JOIN dbo.timItem ON dbo.timItem.itemkey = dbo.Inventory.itemkey