需要在SQL中调试Left Join

时间:2014-07-16 16:30:53

标签: sql ms-access join ms-access-2010

帮助,我是SQL的新手,也是Stack Overflow的新手!

我收到一个“语法错误”,这似乎只发生在第二个左连接(没有它,代码运行正常)。这是我的代码,由Access 2010编译:

SELECT      Billings.[CustomerNumber], 
            Billings.[Customer Name], 
            Billings.[SOP Number], 
            Billings.[Item Number], 
            Sum (Billings.[Extended Price]) AS ['Revenue'], 
            Sum (Billings.QTY) AS ['Quantity']
FROM        [Product Codes]         AS Codes
LEFT JOIN   Billings                ON Codes.[Product Code] = Billings.[Item Number]
LEFT JOIN   [All Rep Assignments]   ON Billings.CustomerNumber = [All Rep Assignments].[Account Number]
GROUP BY    Billings.[CustomerNumber], 
            Billings.[Customer Name], 
            Billings.[SOP Number], 
            Billings.[Item Number]

2 个答案:

答案 0 :(得分:0)

如果显示的SQL来自使用SQL视图的Access查询构建器,请注意Access对空白区域敏感。确保您的线条以空格结尾。 (我注意到最后没有分号,这在该视图中是必需的。)

如果您的代码来自其他地方,例如VBA模块,则使用SQL视图将该SQL移动到Access查询构建器并从那里进行故障排除。 (最后用分号。)更多信息见here

答案 1 :(得分:0)

多个连接时需要括号(每个连接1个)

SQL multiple join statement

SELECT      Billings.[CustomerNumber], 
            Billings.[Customer Name], 
            Billings.[SOP Number], 
            Billings.[Item Number], 
            Sum (Billings.[Extended Price]) AS ['Revenue'], 
            Sum (Billings.QTY) AS ['Quantity']
FROM        (([Product Codes]         AS Codes
LEFT JOIN   Billings                
ON Codes.[Product Code] = Billings.[Item Number])
LEFT JOIN   [All Rep Assignments]   
ON Billings.CustomerNumber = [All Rep Assignments].[Account Number])
GROUP BY    Billings.[CustomerNumber], 
            Billings.[Customer Name], 
            Billings.[SOP Number], 
            Billings.[Item Number]