MS-ACCESS使用LEFT JOIN和INNER JOIN

时间:2014-12-28 07:45:14

标签: sql ms-access join left-join ms-access-2013

SELECT
U.[Current Month], 
U.[Security Name],
U.[CM Return],
(P1.[Return] * P2.[Return] * P3.[Return] * P4.[Return] * P5.[Return] * P6.[Return] * P7.[Return]) AS [7 Month Return],
(P1.[Return] * P2.[Return] * P3.[Return] * P4.[Return] * P5.[Return] * P6.[Return] * P7.[Return] * P8.[Return]) AS [8 Month Return],
(P1.[Return] * P2.[Return] * P3.[Return] * P4.[Return] * P5.[Return] * P6.[Return] * P7.[Return] * P8.[Return] * P9.[Return]) AS [9 Month Return],
(P1.[Return] * P2.[Return] * P3.[Return] * P4.[Return] * P5.[Return] * P6.[Return] * P7.[Return] * P8.[Return] * P9.[Return] * P10.[Return]) AS [10 Month Return],
(P1.[Return] * P2.[Return] * P3.[Return] * P4.[Return] * P5.[Return] * P6.[Return] * P7.[Return] * P8.[Return] * P9.[Return] * P10.[Return] * P11.[Return]) AS [11 Month Return]
FROM ((((((((((((
[Prior Month Returns Tbl] AS U
INNER JOIN [Securities] AS S ON U.[Security Name] = S.[Security Name])
LEFT JOIN [Data 9 17 Monthly] AS P1 ON P1.[Security ID] = S.[ID] AND P1.[Date Month] = DateAdd("m",-1,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P2 ON P2.[Security ID] = S.[ID] AND P2.[Date Month] = DateAdd("m",-2,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P3 ON P3.[Security ID] = S.[ID] AND P3.[Date Month] = DateAdd("m",-3,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P4 ON P4.[Security ID] = S.[ID] AND P4.[Date Month] = DateAdd("m",-4,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P5 ON P5.[Security ID] = S.[ID] AND P5.[Date Month] = DateAdd("m",-5,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P6 ON P6.[Security ID] = S.[ID] AND P6.[Date Month] = DateAdd("m",-6,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P7 ON P7.[Security ID] = S.[ID] AND P7.[Date Month] = DateAdd("m",-7,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P8 ON P8.[Security ID] = S.[ID] AND P8.[Date Month] = DateAdd("m",-8,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P9 ON P9.[Security ID] = S.[ID] AND P9.[Date Month] = DateAdd("m",-9,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P10 ON P10.[Security ID] = S.[ID] AND P10.[Date Month] = DateAdd("m",-10,U.[Current Month]))
LEFT JOIN [Data 9 17 Monthly] AS P11 ON P11.[Security ID] = S.[ID] AND P11.[Date Month] = DateAdd("m",-11,U.[Current Month]))
WHERE S.ID = 14

以上是我想要执行的MS-ACCESS查询。我想要做的是简单的安全ID 14我想要计算[前一个月返回Tbl]中当前月份的[数据9到17个月]表中之前7到11个月返回的乘积。如果我使用内连接,查询工作正常,但如果不是所有以前的11个月记录都出现在[数据9到17每月]中,它将省略结果,这就是为什么我使用左连接但我得到错误,加入表达不支持。请帮忙。我知道这个查询在t-sql中可以正常工作。我应该怎么做,这个查询也可以在MS-Access中使用?

1 个答案:

答案 0 :(得分:2)

在外连接中,MS Access不支持不在两个表之间的条件。在我看来,这是一个非常奇怪的限制。

您可以使用子查询解决它。这是一个例子:

LEFT JOIN
(SELECT P1.*
 FROM [Data 9 17 Monthly] AS P1
 WHERE P1.[Date Month] = DateAdd("m",-1,U.[Current Month]))
) as P1
ON P1.[Security ID] = S.[ID]

或者,您可以使用条件聚合将所有数据合并为一行:

LEFT JOIN
(SELECT [Security ID],
        MAX(IIF(P1.[Date Month] = DateAdd("m", -1, U.[Current Month])), [RETURN], 1) as Return_01,
        MAX(IIF(P1.[Date Month] = DateAdd("m", -2, U.[Current Month])), [RETURN], 1) as Return_02,
        . . .
        MAX(IIF(P1.[Date Month] = DateAdd("m", -12, U.[Current Month])), [RETURN], 1) as Return_12
) as P
ON P.[Security ID] = S.[ID]

然后根据需要调整外部SELECT

或者,您可以升级到更符合ANSI标准的数据库。