ADO中的多个左连接excel

时间:2016-01-11 09:42:16

标签: sql

以下SQL查询有语法错误,我该如何解决?

SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt," & _
      "SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS " & _
      "FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference]" & _
      "LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code]" & _
      "GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];

1 个答案:

答案 0 :(得分:1)

在动态查询中,您需要在LEFT JOINGROUP BY之前添加空格,这在查询中是错过的。因此,在行尾添加空格将起作用。

因此以下查询将起作用:

"SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt, " & _
"SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS " & _
"FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference] " & _
"LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code] " & _
"GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];"

您的实际查询将返回如下所示,它将没有适当的间距。

SELECT [CREDITORS$].[Account Code], [CREDITORS$].[Name], Sum([CREDITORS$]![Base Amount]*-1) AS BillAmt, SUM([SERVICETAX$].[Base Amount]) AS STAMT, Sum(([CREDITORS$]![Base Amount]*-1)-[SERVICETAX$]![Base Amount]) AS TDS FROM [CREDITORS$] LEFT JOIN [SERVICETAX$] ON [CREDITORS$].[Transaction Reference]=[SERVICETAX$].[Transaction Reference]LEFT JOIN [MASTER$] ON [MASTER$].[Account Code]=[CREDITORS$].[Account Code]GROUP BY [CREDITORS$].[Account Code], [CREDITORS$].[Name];