编辑:我需要在 ACCESS 中执行此操作。
我是一个 SQL 处女,非常感谢任何神奇的帮助!
对于简单的 12 个月预测,我使用12 个月双移动平均线。我已设法通过查询 1(如下)拉出单一移动平均线。基于查询 1 创建的表,我编写了另一个查询(查询 2)来获得双移动平均线。
因此,我当前的流程需要两个查询。到目前为止,我将这两个步骤组合成一个查询的努力没有成功。
我的问题:有没有办法在单个查询中计算双移动平均线?
QUERY 1 - 对于单一移动平均线:
SELECT A.*, IIf([A].[VOL]>0,
(SELECT AVG(B.[VOL])
FROM [Turnover] as B
WHERE (B.Code = A.Code) AND (B.YM Between A.YM - 1 AND A.YM - ([12] * 31))),
(SELECT AVG(B.[VOL])
FROM [Turnover] as B
WHERE (B.Code = A.Code) AND (B.YM Between Now() - 31 AND A.YM - ([12] * 31)))) AS [Mvg Avg 1],
INTO [Model 12m]
FROM [Turnover] AS A;
QUERY 2 - 双移动平均线(目前指的是 QUERY 1):
SELECT A.*, IIf([A].[Mvg Avg 1]>0,(SELECT AVG(B.[Mvg Avg 1])
FROM [Model 12m] as B
WHERE (B.Code = A.Code) AND (B.YM Between A.YM - 1 AND A.YM - ([12] * 31))),(SELECT AVG(B.[Mvg Avg 1])
FROM [Model 12m] as B
WHERE (B.Code = A.Code) AND (B.YM Between Now() - 31 AND A.YM - ([12] * 31)))) AS [2 Mvg Avg],
INTO [Model 12m - 2MA]
FROM [Model 12m] AS A;