【问题标题】:Data disappears when I JOIN the final table - SQL当我加入最终表时数据消失 - SQL
【发布时间】:2020-09-26 08:05:02
【问题描述】:

我有这个查询需要完成,我需要返回在特定日期带来商品的客户的姓名和每件商品的名称。

但是,每当我将客户表连接到其他表时,它基本上都会返回为 NULL。里面肯定有数据。

我尝试了一系列不同的连接类型,但似乎没有一个可以链接并保留客户信息。 我的联接类型似乎有问题……也许吧。

提前感谢团队!

SELECT DISTINCT PM.Name AS [ProductModel Name], P.FirstName AS [Customer Name]
FROM AdventureWorksDB.Production.TransactionHistory TH 
    FULL JOIN AdventureWorksDB.Production.Product PP 
    ON TH.ProductID = PP.ProductID 
    FULL JOIN AdventureWorksDB.Production.ProductModel PM 
    ON PP.ProductModelID = PM.ProductModelID
    FULL JOIN AdventureWorksDB.Purchasing.ProductVendor PV
    ON PP.ProductID = PV.ProductID
    FULL JOIN AdventureWorksDB.Purchasing.Vendor V
    ON PV.BusinessEntityID = V.BusinessEntityID
    FULL JOIN AdventureWorksDB.Person.BusinessEntity BE
    ON V.BusinessEntityID = BE.BusinessEntityID
    FULL OUTER JOIN AdventureWorksDB.Person.Person P
    ON BE.BusinessEntityID = P.BusinessEntityID


WHERE PP.SellStartDate = '2007-07-01'

还有输出:



ModelName               |Customer Name
--------------------------------------
All-Purpose Bike Stand  | NULL
Bike Wash               | NULL
Chain                   | NULL
Classic Vest            | NULL
Fender Set - Mountain   | NULL
Front Brakes            | NULL
Front Derailleur        | NULL
Hitch Rack - 4-Bike     | NULL
HL Bottom Bracket       | NULL
...
etc.

【问题讨论】:

    标签: sql ddl dml


    【解决方案1】:

    WHERE 子句中的限制移动到相应连接的ON 子句中:

    SELECT DISTINCT PM.Name AS [ProductModel Name], P.FirstName AS [Customer Name]
    FROM AdventureWorksDB.Production.TransactionHistory TH 
    FULL JOIN AdventureWorksDB.Production.Product PP
        ON TH.ProductID = PP.ProductID AND
           PP.SellStartDate = '2007-07-01'
    -- rest of your current query
    

    您当前的 WHERE 子句将从 Product 表中过滤掉所有销售开始日期不是 2007 年 7 月 1 日的记录。

    【讨论】:

      【解决方案2】:

      FULL JOIN 几乎从不使用——而且几乎从不需要遍历定义良好的适当数据模型。在这种情况下,这是完全不合适的,因为您需要填写所有列才能回答问题。

      您获得NULL 值是因为所有这些FULL JOINs。不匹配的行产生NULL 值。

      我需要返回在某个日期带来物品的客户的姓名和每件物品的名称。

      所以,请使用INNER JOIN。仅当一位客户在该日期多次购买同一产品时,才需要SELECT DISTINCT。我会从一个简单的SELECT开始:

      SELECT PM.Name AS [ProductModel Name], P.FirstName AS [Customer Name]
      FROM AdventureWorksDB.Production.TransactionHistory TH JOIN
           AdventureWorksDB.Production.Product PP 
           ON TH.ProductID = PP.ProductID JOIN
           AdventureWorksDB.Production.ProductModel PM 
           ON PP.ProductModelID = PM.ProductModelID JOIN
           AdventureWorksDB.Purchasing.ProductVendor PV
           ON PP.ProductID = PV.ProductID JOIN
           AdventureWorksDB.Purchasing.Vendor V
           ON PV.BusinessEntityID = V.BusinessEntityID JOIN
           AdventureWorksDB.Person.BusinessEntity BE
           ON V.BusinessEntityID = BE.BusinessEntityID
           AdventureWorksDB.Person.Person P
           ON BE.BusinessEntityID = P.BusinessEntityID
      WHERE PP.SellStartDate = '2007-07-01'
      

      注意:这是使用您在查询中提供的逻辑。不过,基于这个问题,我希望 WHERE 子句是:

      WHERE CONVERT(DATE, TH.TransactionDate) = '2007-07-01'
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-18
        • 1970-01-01
        相关资源
        最近更新 更多