MS Access SQL 查询 - 将所有表与多个表联合 - 重复字段

Posted

技术标签:

【中文标题】MS Access SQL 查询 - 将所有表与多个表联合 - 重复字段【英文标题】:MSAccess SQL Query - Union all with multiple tables - Duplicate field 【发布时间】:2019-03-26 10:19:16 【问题描述】:

我正在尝试从三个查询中获取总数 qryBOMqryLabourqryLaser 并将它们列在一行上。

我创建了一个qryTotals 过滤器,这是我的代码:

SELECT
  Sum(tempTotalLabour) AS TotalLabour,
  Sum(tempTotalCost) AS TotalCost,
  Sum(tempTotalLaser) AS TotalLaser,
  [TotalLabour] + [TotalCost] + [TotalLaser] AS ProductCost

FROM
      (

    SELECT
      Sum([qryLabour].[Labour Mins]) AS tempTotalLabour,
      Sum([qryLabour].[$ Cost]) AS tempTotalCost,
      Sum([qryLabour].[Laser Mins]) AS tempTotalLabour
    FROM
      qryLabour

    union all

    SELECT
      Sum([qryBOM].[Labour Mins]) AS tempTotalLabour,
      Sum([qryBOM].[$ Cost]) AS tempTotalCost,
      Sum([qryBOM].[Laser Mins]) AS tempTotaMaterial
    FROM
      qryBOM

    union all

    SELECT
      Sum([qryLaser].[Labour Mins]) AS tempTotalLabour,
      Sum([qryLaser].[$ Cost]) AS tempTotalCost,
      Sum([qryLaser].[Laser Mins]) AS tempTotalLaser
    FROM
      qryLaser

  ) AS TotalTable;

但我收到了错误 Duplicate Output Alias - 'tempTotalLabour'。请有人帮我解决我哪里出错了,这样我就可以解决这个问题并为将来学习。

干杯 克里斯

【问题讨论】:

这不可能是您正在运行以获取该错误消息的 SQL。该错误指的是tempLabourOutput,它在您的查询中不存在。请包含生成实际错误消息的实际查询。 对不起,我打错了。应该是tempTotalLabour 你必须学会​​校对代码。除了明显的拼写错误和拼写错误之外,这里没有任何问题。 抱歉,我使用的是 Access Query Editor,它都在一行上,我只以上述方式格式化,以便 Stack 用户更容易阅读 - 我应该尽可能地检查一下现在发现问题 【参考方案1】:

你使用了两次tempTotalLabour,我像下面这样单次尝试

SELECT
  Sum(tempTotalLabour) AS TotalLabour,
  Sum(tempTotalCost) AS TotalCost,
  Sum(tempTotalLaser) AS TotalLaser,
 Sum(tempTotalLabour) + Sum(tempTotalCost) + Sum(tempTotalLaser) AS ProductCost

FROM
      (

    SELECT
      Sum([qryLabour].[Labour Mins]) AS tempTotalLabour,
      Sum([qryLabour].[$ Cost]) AS tempTotalCost,
      Sum([qryLabour].[Laser Mins]) as tempTotalLaser

    FROM
      qryLabour

    union all

    SELECT
      Sum([qryBOM].[Labour Mins]) AS tempTotalLabour,
      Sum([qryBOM].[$ Cost]) AS tempTotalCost,
      Sum([qryBOM].[Laser Mins]) AS tempTotalLaser
    FROM
      qryBOM

    union all

    SELECT
      Sum([qryLaser].[Labour Mins]) AS tempTotalLabour,
      Sum([qryLaser].[$ Cost]) AS tempTotalCost,
      Sum([qryLaser].[Laser Mins]) AS tempTotalLaser
    FROM
      qryLaser

  ) AS TotalTable;

【讨论】:

以上是关于MS Access SQL 查询 - 将所有表与多个表联合 - 重复字段的主要内容,如果未能解决你的问题,请参考以下文章

MS Access JOIN 访问表与 SQL Server 表

在 sql (MS-Access) 中编写分组查询

使用 VBA 或 PowerShell 将所有 MS Access SQL 查询导出到文本文件

对 SQL 文件的所有 MS Access 查询

MS Access 2007 中的 SQL 内部连接

MS Access,如何将参数传递给子查询?