0

我有一个包含时间序列数据的访问表,如下所示:

loc | date | value
A    2/11/07 50
A    2/12/07 45
A    2/13/07 23
B    2/11/07 34
B    2/12/07 46
B    2/13/07 56
C   ....... ...
...
D..........
.....

我想获得 Z, (value - avg(values)/stDev(values), 每个组在不同时间段的值,因此 20 z 值将考虑过去 20 天的值,过去 60 天的 60 天天等。我还想选择最近一天的 z 值,所以结果如下所示:

loc | date | value | 20Day zValue | 60Day ZValue | 120 day Zvalue
A   2/13/07   23       .04           .09            .6
B   2/13/07   56         .87         .54            .96
C .....................
4

2 回答 2

1

尝试这个:

SELECT 
    a.*,
    b.20Day_zValue,
    c.60Day_zValue,
    d.120Day_zValue
FROM
(
    SELECT aa.loc, aa.date, aa.value
    FROM tbl aa
    INNER JOIN 
    (
        SELECT loc, MAX(date) AS maxdate
        FROM tbl
        GROUP BY loc
    ) bb ON aa.loc = bb.loc AND aa.date = bb.maxdate
) a
INNER JOIN 
(
    SELECT loc, AVG(value)/StDev(value) AS 20Day_zValue
    FROM tbl
    WHERE date >= DateAdd('d', -20, Date())
    GROUP BY loc
) b ON a.loc = b.loc
INNER JOIN 
(
    SELECT loc, AVG(value)/StDev(value) AS 60Day_zValue
    FROM tbl
    WHERE date >= DateAdd('d', -60, Date())
    GROUP BY loc
) c ON a.loc = c.loc
INNER JOIN 
(
    SELECT loc, AVG(value)/StDev(value) AS 120Day_zValue
    FROM tbl
    WHERE date >= DateAdd('d', -120, Date())
    GROUP BY loc
) d ON a.loc = d.loc
于 2012-07-16T11:05:49.387 回答
0

我想出了一个解决方案,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 分数的日期范围。

于 2012-07-25T08:54:39.710 回答