MSAccess SQL查询-将所有与多个表合并-重复字段

时间:2019-03-26 10:19:17

标签: sql ms-access union-all

我正在尝试从三个查询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'。请有人可以帮助我解决我要去的地方,以便我可以解决此问题并为将来学习。

欢呼 克里斯

1 个答案:

答案 0 :(得分:2)

您两次使用过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;