sql时,ddl-data就消失了

htzpubme  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(238)

我有这个查询,我需要完成,我需要返回的客户谁带来了在某个日期和每个项目的名称项目的名称。
但是,每当我将customer表连接到其他表时,它基本上返回为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.
z6psavjg

z6psavjg1#

将限制移动到 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日。

tvokkenx

tvokkenx2#

FULL JOIN 几乎从未使用过,也几乎从未需要遍历定义良好的数据模型。在这种情况下,这是完全不合适的,因为您需要填写所有列来回答问题。
你越来越 NULL 因为所有这些 FULL JOIN s。不匹配的行产生 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'

相关问题