工作查询的轻微更改会导致JOIN错误

时间:2017-12-22 20:16:33

标签: sql ms-access

这让我整天都在摸不着头脑。我有多个查询,我试图使用计算字段加入。第一个查询按预期运行,但MS Access设计视图不起作用,这不是一个大问题,因为我仍然可以在SQL视图中编辑。

SELECT PM_qryBOM_1.parentid AS LVL0,
       PM_qryBOM_1.childid AS LVL1,
       [PM_qryBOM_1].[parent_part_no] & "/" & [PM_qryBOM_1].[child_part_no] AS BOM_Address,
       PM_qryBOM_1.qty,
       [PM_qryBOM_1].[parentid] & "/" & [PM_qryBOM_1].[childid] AS BOM_ID,
       sol_avgcost.avgcost_material,
       sol_avgcost.avgcost_labor,
       sol_avgcost.avgcost_sub,
       cm_qrypndj_avg.avgofunit_hrs,
       PM_qryBOM_1.parentid  AS Parent_BOM_ID,
       PM_qryBOM_1.parent_part_no AS Parent_BOM_Address,
       pm_qrybom_lvl1_costroll.mult
FROM   (((pm_qrybom AS PM_qryBOM_1
          LEFT JOIN pm_qrybom AS PM_qryBOM_t
                 ON PM_qryBOM_1.parentid = PM_qryBOM_t.childid)
         LEFT JOIN sol_avgcost
                ON PM_qryBOM_1.child_part_no = sol_avgcost.part_no)
        LEFT JOIN cm_qrypndj_avg
               ON PM_qryBOM_1.child_part_no = cm_qrypndj_avg.part_no)
       LEFT JOIN pm_qrybom_lvl1_costroll
              ON [PM_qryBOM_1].[parentid] & "/" & [PM_qryBOM_1].[childid] =
                 pm_qrybom_lvl1_costroll.parent_bom_id
WHERE  (( ( PM_qryBOM_t.childid ) IS NULL ))
ORDER  BY [PM_qryBOM_1].[parent_part_no] & "/" & [PM_qryBOM_1].[child_part_no];

我有多个其他查询,我想以这种方式JOIN。所以我尝试将相同的语法应用于我的下一个查询,但是我收到了错误

  

不支持JOIN表达式。

 SELECT pm_qrybom_lvl0.lvl0,
       pm_qrybom.childid AS LVL1,
       pm_qrybom.qty,
       [pm_qrybom_lvl0].[bom_address] & "/" & [pm_qrybom].[child_part_no] AS BOM_Address,
       [pm_qrybom_lvl0].[bom_id] & "/" & [pm_qrybom].[childid] AS BOM_ID,
       pm_qrybom_lvl0.bom_address AS Parent_BOM_Address,
       pm_qrybom_lvl0.bom_id AS Parent_BOM_ID,
       sol_avgcost.avgcost_material,
       cm_qrypndj_avg.avgofunit_hrs,
       cm_collections.cpq_material,
       pm_qrybom_lvl1_costroll.mult,
       Iif([cm_collections].[cpq_material] <> 0, [cm_collections].[cpq_material], 
           Iif([mult] = 0, [sol_avgcost].[avgcost_material], [mult])) AS Material_Calc
FROM   ((((pm_qrybom_lvl0
           INNER JOIN pm_qrybom
                   ON pm_qrybom_lvl0.lvl0 = pm_qrybom.parentid)
          LEFT JOIN sol_avgcost
                 ON pm_qrybom.child_part_no = sol_avgcost.part_no)
         LEFT JOIN cm_qrypndj_avg
                ON pm_qrybom.child_part_no = cm_qrypndj_avg.part_no)
        LEFT JOIN cm_collections
               ON pm_qrybom.childid = cm_collections.partid)
       LEFT JOIN pm_qrybom_lvl1_costroll
              ON [pm_qrybom_lvl0].[bom_id] & "/" & [pm_qrybom].[childid] =
                 pm_qrybom_lvl1_costroll.parent_bom_id
ORDER  BY [pm_qrybom_lvl0].[bom_address] & "/" & [pm_qrybom].[child_part_no];  

两个查询的语法都是相同的。只有引用表名称和字段名称不同。那么为什么我可以在一个查询中加入表达式但不能在下一个查询中加入?我用Google搜索了这个问题,有些人建议在ON语句中的JOIN之后加上括号。我试过了,它没有解决问题。

任何建议都会受到赞赏,因为我完全坚持这一点。

1 个答案:

答案 0 :(得分:0)

这是JOIN语句末尾的连接,与您尝试使用的INNER JOIN不匹配。尝试使用LEFT JOIN。然后,在WHERE语句中,通过排除NULL来删除右侧不匹配的那些:

SELECT pm_qrybom_lvl0.lvl0,
       pm_qrybom.childid AS LVL1,
       pm_qrybom.qty,
       [pm_qrybom_lvl0].[bom_address] & "/" & [pm_qrybom].[child_part_no] AS BOM_Address,
       [pm_qrybom_lvl0].[bom_id] & "/" & [pm_qrybom].[childid] AS BOM_ID,
       pm_qrybom_lvl0.bom_address AS Parent_BOM_Address,
       pm_qrybom_lvl0.bom_id AS Parent_BOM_ID,
       sol_avgcost.avgcost_material,
       cm_qrypndj_avg.avgofunit_hrs,
       cm_collections.cpq_material,
       pm_qrybom_lvl1_costroll.mult,
       Iif([cm_collections].[cpq_material] <> 0, [cm_collections].[cpq_material], 
           Iif([mult] = 0, [sol_avgcost].[avgcost_material], [mult])) AS Material_Calc
FROM   ((((pm_qrybom_lvl0
           LEFT JOIN pm_qrybom
                   ON pm_qrybom_lvl0.lvl0 = pm_qrybom.parentid)
          LEFT JOIN sol_avgcost
                 ON pm_qrybom.child_part_no = sol_avgcost.part_no)
         LEFT JOIN cm_qrypndj_avg
                ON pm_qrybom.child_part_no = cm_qrypndj_avg.part_no)
        LEFT JOIN cm_collections
               ON pm_qrybom.childid = cm_collections.partid)
       LEFT JOIN pm_qrybom_lvl1_costroll
              ON [pm_qrybom_lvl0].[bom_id] & "/" & [pm_qrybom].[childid] =
                 pm_qrybom_lvl1_costroll.parent_bom_id
       WHERE pm_qrybom.parentid IS NOT NULL
ORDER  BY [pm_qrybom_lvl0].[bom_address] & "/" & [pm_qrybom].[child_part_no];

如果这不能提供您想要的结果,您还可以尝试嵌套一个select语句,该语句为您提供from语句中的连接。

类似的东西:

SELECT iq.lvl0,
       iq.childid AS LVL1,
       iq.qty,
       iq.BOM_Address,
       iq.BOM_ID,
       iq.Parent_BOM_Address,
       iq.Parent_BOM_ID,
       sol_avgcost.avgcost_material,
       cm_qrypndj_avg.avgofunit_hrs,
       cm_collections.cpq_material,
       pm_qrybom_lvl1_costroll.mult,
       Iif([cm_collections].[cpq_material] <> 0, [cm_collections].[cpq_material], 
           Iif([mult] = 0, [sol_avgcost].[avgcost_material], [mult])) AS Material_Calc
FROM ((((SELECT pm_qrybom_lvl0.lvl0, pm_qrybom.childid, pm_qrybom.qty, 
        [pm_qrybom_lvl0].[bom_address] & "/" & [pm_qrybom].[child_part_no] AS BOM_Address,
        [pm_qrybom_lvl0].[bom_id] & "/" & [pm_qrybom].[childid] AS bom_id,
        pm_qrybom_lvl0.bom_address AS Parent_BOM_Address,
        pm_qrybom_lvl0.bom_id AS Parent_BOM_ID
        FROM pm_qrybom_lvl0 LEFT JOIN pm_qrybom
                   ON pm_qrybom_lvl0.lvl0 = pm_qrybom.parentid
        WHERE pm_qrybom.parentid IS NOT NULL

            ) AS iq
          LEFT JOIN sol_avgcost
                 ON iq.child_part_no = sol_avgcost.part_no)
         LEFT JOIN cm_qrypndj_avg
                ON iq.child_part_no = cm_qrypndj_avg.part_no)
        LEFT JOIN cm_collections
               ON iq.childid = cm_collections.partid)
       LEFT JOIN pm_qrybom_lvl1_costroll
              ON iq.bom_id =
                 pm_qrybom_lvl1_costroll.parent_bom_id
祝你好运。