Access SQL Query:Left Join不显示所有Left Table行,这是一个Union Query

时间:2017-12-28 16:57:29

标签: sql ms-access left-join union

我有两个查询和一个我想要合并的表:

  • [产品需求](表)
  • [NA Supply](查询)
  • [欧盟供应](查询)

这三个字段相同:

  • 日期
  • 区域
  • 产品
  • 数量

虽然,我会说在供应查询中,我强制DateRegion为静态值,而ProductQuantity则从表中提取。例如:

SELECT 
  DateValue("12/1/2017") AS [Date], 
  "North America" AS [Region],  <-- Would be "Europe" for [EU Supply]
  [NA Inv Report].[Product], 
  [NA Inv Report].[Quantity]
FROM [NA Inv Report]   <-- Would be "EU Inv Report" for [EU Supply]
  WHERE ((([NA Inv Report].[Quantity])>0));

然后我有一个基本的联合查询,名为[uQuery: Combo]

SELECT 
  [Product Demand].[Date] AS [Date],
  [Product Demand].[Region] AS [Region],
  [Product Demand].[Product] AS [Product]
FROM [Product Demand]
UNION SELECT 
  [NA Supply].[Date] AS [Date],
  [NA Supply].[Region] AS [Region],
  [NA Supply].[Product] AS [Product]
FROM [NA Supply]
UNION SELECT 
  [EU Supply].[Date] AS [Date],
  [EU Supply].[Region] AS [Region],
  [EU Supply].[Product] AS [Product]
FROM [EU Supply];

然后,我将Union Query拉入新查询;

SELECT 
  [uQuery: Combo].Date, 
  [uQuery: Combo].Region, 
  [uQuery: Combo].Product
FROM [uQuery: Combo];

这显示了一切!

但这是令人困惑的事情;如果我LEFT JOIN Product Demand仍然显示所有内容。 但是如果我左右加入NA SupplyEU Supply,则会将结果过滤到添加的表格。如果我添加BOTH供应表,那么它将不会显示任何结果。

SELECT 
  [uQuery: Combo].Date, 
  [uQuery: Combo].Region, 
  [uQuery: Combo].Product
FROM (([uQuery: Combo] 
  LEFT JOIN [Product Demand] ON  <-- Adding just this will show everything
    ([uQuery: Combo].Product = [Product Demand].Product) AND 
    ([uQuery: Combo].Region = [Product Demand].Region) AND 
    ([uQuery: Combo].Date = [Product Demand].Date)) 
  LEFT JOIN [NA Supply] ON  <-- Adding this will only show matching results for [NA Supply]
    ([uQuery: Combo].Product = [NA Supply].Product) AND 
    ([uQuery: Combo].Region = [NA Supply].Region) AND 
    ([uQuery: Combo].Date = [NA Supply].Date)) 
  LEFT JOIN [EU Supply] ON  <-- Adding this will hide all results
    ([uQuery: Combo].Product = [EU Supply].Product) AND 
    ([uQuery: Combo].Region = [EU Supply].Region) AND 
    ([uQuery: Combo].Date = [EU Supply].Date);

1 个答案:

答案 0 :(得分:1)

所有结果都会消失,因为您要强制NA和EU区域匹配,因为您将它们硬编码为不同的值。即使使用LEFT JOIN NA Supply 也不会按逻辑"North America" <> "Europe"返回任何行。

考虑分离 Demand Supply 数据,然后加入最终查询,其中匹配可能在需求和供应区域之间。

联盟查询

SELECT 
  [NA Supply].[Date] AS [Date],
  [NA Supply].[Region] AS [Region],
  [NA Supply].[Product] AS [Product]
FROM [NA Supply]
UNION 
SELECT 
  [EU Supply].[Date] AS [Date],
  [EU Supply].[Region] AS [Region],
  [EU Supply].[Product] AS [Product]
FROM [EU Supply]

最终查询

SELECT 
  u.[Date], 
  u.Region, 
  u.Product,
  u.Quantity AS SupplyQty,
  p.Quantity AS DemandQty
FROM [uQuery: Combo] u
LEFT JOIN [Product Demand] p ON
  u.[Date] = p.[Date] AND 
  u.Region = p.Region AND 
  u.Product = p.Product

完整外部加入查询

现在,您可能希望完整的外部联接包含需求而不匹配供应。 由于MS Access不直接支持FULL OUTER JOIN,因此可以通过RIGHT JOIN的并集来实现等效结果(假设两个表中都不存在重复的行)。

SELECT u.[Date], u.Region, u.Product, u.Quantity As SupplyQty, p.Quantity As DemandQty
FROM [uQuery: Combo] u
LEFT JOIN [Product Demand] p ON
  u.[Date] = p.[Date] AND u.Region = p.Region AND u.Product = p.Product

UNION

SELECT u.[Date], u.Region, u.Product, u.Quantity, p.Quantity
FROM [uQuery: Combo] u
RIGHT JOIN [Product Demand] p ON
  u.[Date] = p.[Date] AND u.Region = p.Region AND u.Product = p.Product

ASIDE:理想情况下,您希望维护一个 供应源或一个 Inv报告,因为UNION可能会出现性能问题,尤其是用于其他查询,例如使用UNION的建议全外连接。

在数据库架构中,您不希望复制不同类别​​的结构,例如 NA EU ,而是将相似数据保存在同一对象中,其中数据值变化的指标字段。数据库表不是电子表格选项卡,而是规范化的相关逻辑数据分组,可避免冗余。