我想出了一个解决方案,zane bien 的回答是一个好的开始。主要问题是 ms access 对多个连接的要求。基本的括号结构是这样的:
Select a,b,c
FROM
(((Table1 Inner Join Table2 ON a = b)
Inner Join Table3 ON a = c)
Inner Join Table4 ON a = d)
我的问题的解决方案是:
SELECT A.loc AS Location, A.value AS Value,
(A.value - B.OneMonthAvg) / B.OneMonthStdev AS OneMonthZscore,
(A.value - C.ThreeMonthAvg) / C.ThreeMonthStdev AS ThreeMonthZscore,
(A.value - D.SixMonthAvg) / D.SixMonthStdev AS SixMonthZscore,
(A.value - E.OneYearAvg) / E.OneYearStdev AS OneYearZscore,
(A.value - F.TwoYearAvg) / F.TwoYearStdev AS TwoYearZscore,
(A.value - G.ThreeYearAvg) / G.ThreeYearStdev AS ThreeYearZscore
FROM
(((((((tbl AS A
INNER JOIN
(SELECT loc, AVG(value) AS OneMonthAvg, STDEV(value) AS OneMonthStdev
FROM tbl
WHERE date >= DateAdd('m', -1, Date())
GROUP BY loc)
AS B ON A.loc = B.loc)
INNER JOIN
(SELECT loc, AVG(value) AS ThreeMonthAvg, STDEV(value) AS ThreeMonthStdev
FROM tbl
WHERE date >= DateAdd('m', -3, Date())
GROUP BY loc)
AS C ON A.loc = C.loc)
INNER JOIN
(SELECT loc, AVG(value) AS SixMonthAvg, STDEV(value) AS SixMonthStdev
FROM tbl
WHERE date >= DateAdd('m', -6, Date())
GROUP BY loc)
AS D ON A.loc = D.loc)
INNER JOIN
(SELECT loc, AVG(value) AS OneYearAvg, STDEV(value) AS OneYearStdev
FROM tbl
WHERE date >= DateAdd('yyyy', -1, Date())
GROUP BY loc)
AS E ON A.loc = E.loc)
INNER JOIN
(SELECT loc, AVG(value) AS TwoYearAvg, STDEV(value) AS TwoYearStdev
FROM tbl
WHERE date >= DateAdd('yyyy', -2, Date())
GROUP BY loc)
AS F ON A.loc = F.loc)
INNER JOIN
(SELECT loc, AVG(value) AS ThreeYearAvg, STDEV(value) AS ThreeYearStdev
FROM tbl
WHERE date >= DateAdd('yyyy', -3, Date())
GROUP BY loc)
AS G ON A.loc = G.loc)
Where A.date = Date()
我更改了想要获取 z 分数的日期范围。