嵌套连接 Excel VBA (ADODB) 结果 "JOIN expression not supported"
Nested Join in Excel VBA (ADODB) Results In "JOIN expression not supported"
我有一系列的三个表,我想在 Excel VBA 应用程序中使用 ADODB 将它们连接在一起。我正在使用以下查询,这会导致 "JOIN expression not supported" 错误:
SELECT tb1.date,
tb1.longID,
tb1.fld1,
tb2.fld2,
tb3.shortID,
SUM(tb1.fld3) AS three,
SUM(tb1.fld4) AS four,
SUM(tb3.fld5) AS five
FROM ([Table1$] AS tb1 LEFT JOIN [Table2$] AS tb2 ON tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3
ON (tb3.shortID = tb2.shortID AND tb1.date = tb3.date)
GROUP BY tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2
如果我省略 shortID
列对,则查询工作正常。如果我省略 date
列对,查询工作正常。但是一旦我将两者结合起来,那就是我 运行 遇到问题的时候。
如有任何帮助,我们将不胜感激!
谢谢。
尽量让查询的 ON 部分内的所有内容都在括号内。
您的 JOIN 操作中的 ON 语句不完整或包含太多表。您可能希望将 ON 表达式放在 WHERE 子句中。
SELECT tb1.date,
tb1.longID,
tb1.fld1,
tb2.fld2,
tb3.shortID,
SUM(tb1.fld3) AS three,
SUM(tb1.fld4) AS four,
SUM(tb3.fld5) AS five
FROM
[Table1$] AS tb1
LEFT JOIN [Table2$] AS tb2 ON (tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3 ON (tb3.shortID = tb2.shortID)
WHERE tb1.date = tb3.date
GROUP BY tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2
ON子句的目的是连接2个表,但是你试图同时连接3个表ON (tb3.shortID = tb2.shortID AND tb1.date = tb3.date)
。您可以通过两种方式解决这个问题:
将ON的一部分移到WHERE子句中,只涉及2个表
...
FROM ([Table1$] AS tb1
LEFT JOIN [Table2$] AS tb2
ON tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3
ON tb2.shortID = tb3.shortID
WHERE tb1.date = tb3.date
...
使用子查询
SELECT
x.date,
x.longID,
x.fld1,
x.fld2,
tb3.shortID,
SUM(x.fld3) AS three,
SUM(x.fld4) AS four,
SUM(tb3.fld5) AS five
FROM
(SELECT
tb1.date, tb1.longID, tb1.fld1,
tb2.fld2
FROM
[Table1$] AS tb1
LEFT JOIN [Table2$] AS tb2
ON tb1.longID = tb2.longID
) x
LEFT JOIN [Table3$] AS tb3
ON (x.shortID = tb3.shortID AND x.date = tb3.date)
GROUP BY
x.date, x.longID, x.fld1, x.fld2, tb3.shortID
我有一系列的三个表,我想在 Excel VBA 应用程序中使用 ADODB 将它们连接在一起。我正在使用以下查询,这会导致 "JOIN expression not supported" 错误:
SELECT tb1.date,
tb1.longID,
tb1.fld1,
tb2.fld2,
tb3.shortID,
SUM(tb1.fld3) AS three,
SUM(tb1.fld4) AS four,
SUM(tb3.fld5) AS five
FROM ([Table1$] AS tb1 LEFT JOIN [Table2$] AS tb2 ON tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3
ON (tb3.shortID = tb2.shortID AND tb1.date = tb3.date)
GROUP BY tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2
如果我省略 shortID
列对,则查询工作正常。如果我省略 date
列对,查询工作正常。但是一旦我将两者结合起来,那就是我 运行 遇到问题的时候。
如有任何帮助,我们将不胜感激!
谢谢。
尽量让查询的 ON 部分内的所有内容都在括号内。
您的 JOIN 操作中的 ON 语句不完整或包含太多表。您可能希望将 ON 表达式放在 WHERE 子句中。
SELECT tb1.date,
tb1.longID,
tb1.fld1,
tb2.fld2,
tb3.shortID,
SUM(tb1.fld3) AS three,
SUM(tb1.fld4) AS four,
SUM(tb3.fld5) AS five
FROM
[Table1$] AS tb1
LEFT JOIN [Table2$] AS tb2 ON (tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3 ON (tb3.shortID = tb2.shortID)
WHERE tb1.date = tb3.date
GROUP BY tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2
ON子句的目的是连接2个表,但是你试图同时连接3个表ON (tb3.shortID = tb2.shortID AND tb1.date = tb3.date)
。您可以通过两种方式解决这个问题:
将ON的一部分移到WHERE子句中,只涉及2个表
... FROM ([Table1$] AS tb1 LEFT JOIN [Table2$] AS tb2 ON tb1.longID = tb2.longID) LEFT JOIN [Table3$] AS tb3 ON tb2.shortID = tb3.shortID WHERE tb1.date = tb3.date ...
使用子查询
SELECT x.date, x.longID, x.fld1, x.fld2, tb3.shortID, SUM(x.fld3) AS three, SUM(x.fld4) AS four, SUM(tb3.fld5) AS five FROM (SELECT tb1.date, tb1.longID, tb1.fld1, tb2.fld2 FROM [Table1$] AS tb1 LEFT JOIN [Table2$] AS tb2 ON tb1.longID = tb2.longID ) x LEFT JOIN [Table3$] AS tb3 ON (x.shortID = tb3.shortID AND x.date = tb3.date) GROUP BY x.date, x.longID, x.fld1, x.fld2, tb3.shortID