计算每组的前N个值

时间:2012-07-16 09:56:05

标签: sql ms-access

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

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,(值 - 平均值(值)/ stDev(值),每组在不同时间段的值,因此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 .....................

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我想出了一个解决方案,zane bien回答是一个好的开始。主要问题是ms访问需要多个连接。基本包围结构是这样的:

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分数的日期范围。