访问查询:当我将表达式命名/别名与继承字段相同时,值将消失

时间:2017-11-30 17:18:53

标签: sql ms-access

我有一个从另一个人那里获取数据的查询。这是我在SQL中的SELECT语句:

SELECT
    [Orders].Date,
    [Orders].[Part Number],
    [Orders].Region

使用它,我的列标题如下:

  Date  ||  [Orders].[Part Number]  ||     Region
  12/1  ||         AAA001           ||  North America
  12/1  ||         BBB002           ||  North America
  12/1  ||         CCC003           ||  North America

请注意,日期区域标记为一个字,而零件编号具有表单/字段格式。为了避免这种情况,我添加了一个别名。

SELECT
    [Orders].Date,
    [Orders].[Part Number] AS [Part Number],
    [Orders].Region

当我这样做时,标题会正常显示,但我的所有值都会消失:

  Date  ||  Part Number  ||     Region
  12/1  ||               ||  North America
  12/1  ||               ||  North America
  12/1  ||               ||  North America

如果我将其命名为其他任何内容(即使我只是添加" s"最后制作"部件号 s "),值重新出现。

  Date  ||  Part Numbers  ||     Region
  12/1  ||     AAA001     ||  North America
  12/1  ||     BBB002     ||  North America
  12/1  ||     CCC003     ||  North America

任何人都知道我可以开始尝试解决这个问题吗?

修改这是我的完整查询。我想在这里我应该注意,前三个字段是从Union查询中提取的,如果这很重要的话。为简单起见,名称在上面进行了更改,但现在它们的实际名称已经改变。我遇到的问题是[uQuery Forecast / Orders]。[Part Number]。

SELECT 
    [uQuery Forecast/Orders].Date, 
    [uQuery Forecast/Orders].[Part Number], 
    [uQuery Forecast/Orders].Region, 
    Val(IIf(IsNull([Orders].[QTY]),0,[Orders].[QTY])) AS [Order QTY], 
    Val(IIf(IsNull([Forecast].[QTY]),0,[Forecast].[QTY])) AS [Forecast QTY], 
    Val(IIf(IsNull([Builds].[QTY]),0,[Builds].[QTY])) AS [Build QTY]
FROM (([uQuery Forecast/Orders] 
    LEFT JOIN [Orders] ON 
        ([uQuery Forecast/Orders].[Part Number] = [Orders].[Part Number]) AND
        ([uQuery Forecast/Orders].[Date] = [Orders].[Date])) 
    LEFT JOIN [Forecast] ON 
        ([uQuery Forecast/Orders].[Part Number] = [Forecast].[Part Number]) AND
        ([uQuery Forecast/Orders].[Date] = [Forecast].[Date])) 
    LEFT JOIN [Builds] ON 
        ([uQuery Forecast/Orders].Date = [Builds].[Date]) AND
        ([uQuery Forecast/Orders].[Part Number] = [Builds].[Part Number])
GROUP BY 
    [uQuery Forecast/Orders].Date, 
    [uQuery Forecast/Orders].[Part Number], 
    [uQuery Forecast/Orders].Region, 
    Val(IIf(IsNull([Orders].[QTY]),0,[Orders].[QTY])), 
    Val(IIf(IsNull([Forecast].[QTY]),0,[Forecast].[QTY])), 
    Val(IIf(IsNull([Builds].[QTY]),0,[Builds].[QTY]));

编辑2

这是我的联盟查询。它的唯一目的是列出日期↔零件号↔区域的任何和所有独特组合。我只有3个区域,大约1000个零件号。日期从9/1/17开始,所以即使在20年后,只有7305个条目。总之,这最多可达22,000,000行。

SELECT 
    [Orders].[Date] AS [Date], 
    [Orders].[Part Number] AS [Part Number], 
    [Orders].[Region] AS [Region]
FROM [Orders]
UNION SELECT 
    [Forecast].[Date], 
    [Forecast].[Part Number], 
    [Forecast].[Region]
FROM [Forecast]
UNION SELECT 
    [Builds].[Date], 
    [Builds].[Part Number], 
    [Builds].[Region]
FROM [Builds];

2 个答案:

答案 0 :(得分:1)

您的SQL维护三个基础表,每个表都包含部件号字段:订单预测构建。因此,在外部查询中存在命名冲突,Access在查询的列名称中显示由句点限定的表名。对于日期 QTY ,如果您将它们包含在外部查询SELECT中,则会出现相同的问题。

要解决考虑使用基础表中的列别名来更改原始部件号并在ON子句中使用这些名称。请参阅使用派生表调整查询,并使用表别名。您也可以将这些嵌套的SELECT保存为自己的查询,并在此处引用它们。

SELECT 
    u.Date, 
    u.[Part Number], 
    u.Region, 
    Val(IIf(IsNull(o.[QTY]), 0, o.[QTY])) AS [Order QTY], 
    Val(IIf(IsNull(f.[QTY]), 0, f.[QTY])) AS [Forecast QTY], 
    Val(IIf(IsNull(b.[QTY]), 0, b.[QTY])) AS [Build QTY]
FROM (([uQuery Forecast/Orders] u
    LEFT JOIN (SELECT [Part Number] As [O_Part_Number], [QTY], [Date] FROM [Orders]) o 
      ON  (u.[Part Number] = o.[O_Part Number]) 
      AND (u.[Date] = o.[Date])) 
    LEFT JOIN (SELECT [Part Number] As [F_Part_Number], [QTY], [Date] FROM [Forecast]) f
      ON  (u.[Part Number] = f.[F_Part_Number])
      AND (u.[Date] = f.[Date])) 
    LEFT JOIN (SELECT [Part Number] As [B_Part_Number], [QTY], [Date] FROM [Builds]) b 
      ON  (u.[Part Number] = b.[B_Part_Number])
      AND (u.[Date] = b.[Date]) 
GROUP BY 
    u.Date, 
    u.[Part Number], 
    u.Region, 
    Val(IIf(IsNull(o.[QTY]), 0, o.[QTY])), 
    Val(IIf(IsNull(f.[QTY]), 0, f.[QTY])), 
    Val(IIf(IsNull(b.[QTY]), 0, b.[QTY]));

答案 1 :(得分:0)

首先,我建议对您的数据库运行Compact/Repair操作并验证数据的完整性。根据我的经验,损坏的数据库可能会出现意外行为。

其次,修改您的UNION查询以从第一个AS中删除SELECT语句:

SELECT [Date], [Part Number], [Region] FROM Builds
UNION SELECT [Date], [Part Number], [Region] FROM Forecast
UNION SELECT [Date], [Part Number], [Region] FROM Orders;

我仍然相信你只需要你的联合查询的DISTINCT结果,如下例所示:

SELECT u.DATE
    ,u.[Part Number]
    ,u.Region
    ,Val(IIf(IsNull(o.[QTY]), 0, o.[QTY])) AS [Order QTY]
    ,Val(IIf(IsNull(f.[QTY]), 0, f.[QTY])) AS [Forecast QTY]
    ,Val(IIf(IsNull(b.[QTY]), 0, b.[QTY])) AS [Build QTY]
FROM (
    (
        (SELECT DISTINCT [Date], [Part Number], [Region] from [uQuery Forecast/Orders]) AS u 
        LEFT JOIN Orders AS o ON (u.[Date] = o.[Date])
            AND (u.[Part Number] = o.[Part Number])
        ) LEFT JOIN Forecast AS f ON (u.[Date] = f.[Date])
        AND (u.[Part Number] = f.[Part Number])
    )
LEFT JOIN Builds AS b ON (u.[Date] = b.[Date])
    AND (u.[Part Number] = b.[Part Number]);

为了在同一行显示每个QTY的结果,您需要在联合查询中使用DISTINCT关键字,以防止同一Product Number / {{的多行1}}对。