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)、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 アクセスの要件でした。基本的なブラケット構造は次のようになります。

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 に答える