【问题标题】:tsql : how do I join this table?tsql:我如何加入这个表?
【发布时间】:2009-03-10 01:39:57
【问题描述】:

我有如下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 的表“tiitem”的代理键,我将如何将它添加到它只会通过 itemkey 查找 LastUnitCost 字段的地方?如果没有与 timeitem 表对应的 LastUnitCost,则它只是 null..

每次我尝试将其添加到 Inventory.itemkey = timItem.itemkey 上的 timItem 的 Inner Join 中时,它都会影响 timItemClass.ItemClassID 字段,其中一些类别不再显示。

提前致谢。

【问题讨论】:

    标签: sql tsql join


    【解决方案1】:

    Inventory.LastUnitCost 添加到SELECT 列表中

    并将这一行添加到查询的末尾:

    dbo.Inventory LEFT OUTER JOIN dbo.timItem ON dbo.timItem.itemkey = dbo.Inventory.itemkey
    

    【讨论】:

    • 您可能还希望将 ISNULL 或 COALESCE 与选择项一起使用以获得更合理的默认值,因为将空值留在那里有时会导致......奇怪......客户端代码。
    • 您不需要某种连接来将其链接到查询中吗?
    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多