简体   繁体   English

每天使用多个值计算 30 天的滚动平均值

[英]Calculating a rolling 30 day average with multiple values per day

I have a table that looks something like this:我有一张看起来像这样的表:

city城市 date日期 value价值
DC直流电 2020-01-01 2020-01-01 10 10
DC直流电 2020-01-01 2020-01-01 23 23
DC直流电 2020-01-02 2020-01-02 43 43
NYC纽约市 2020-01-01 2020-01-01 43 43
NYC纽约市 2020-01-02 2020-01-02 23 23
NYC纽约市 2020-01-03 2020-01-03 10 10

There are multiple values per city, per date.每个城市、每个日期有多个值。 I'm having a hard time calculating the forward looking 30 day average because the condition in the rolling average should be based on the date and not based on the number of rows.我很难计算前瞻性的 30 天平均值,因为滚动平均值中的条件应该基于日期而不是基于行数。 I want the output to be something like this:我希望 output 是这样的:

city城市 date日期 value价值
DC直流电 2020-01-01 2020-01-01 25.33 25.33
DC直流电 2020-01-02 2020-01-02 43 43
NYC纽约市 2020-01-01 2020-01-01 25.33 25.33
NYC纽约市 2020-01-02 2020-01-02 16.5 16.5
NYC纽约市 2020-01-03 2020-01-03 10 10

So I can't do something like this:所以我不能做这样的事情:

AVG(value) OVER (
           PARTITION BY city, date
           ORDER BY date DESC
           ROWS BETWEEN 31 PRECEDING AND 1 PRECEDING
           )

joining the table on itself like this:像这样加入自己的表格:

SELECT t1.city, t1.date, avg(t2.values)
FROM table1 t1
JOIN table1 t2 ON t1.city=t2.city 
    AND t2.date < DATEADD('day',31,t1.date)
    AND t2.date >= t1.date

isn't an option because it's quite large and takes forever.不是一个选择,因为它很大并且需要很长时间。 How do I do this?我该怎么做呢?

You want to use RANGE instead of ROWS in your window frame.您想在 window 框架中使用 RANGE 而不是 ROWS。 But that requires ordering by a number.但这需要按号码订购。 So you need to do:所以你需要做:

over (... order by datediff(date,'1970-01-01') asc range between 31 preceding and 1 following)

(Though I think you want 0 following?) (虽然我认为你想要 0 关注?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM