我正在尝试学习SQL,所以请耐心等待。我正在使用PostgreSQL 9.3
我想根据日期窗口对一列进行平均。我能够编写窗口函数来完成一个集合,interval但是我希望能够随着不断增长做到这一点interval。我的意思是:
interval
average values from date_0 to date_1 average values from date_0 to date_2 average values from date_0 to date_3 ..... so date date_0 stays the same and date_x grows and creates a larger sample
我假设有一个比对我要平均的每个范围运行查询更好的方法。任何建议表示赞赏。谢谢你。
我正在尝试创建均匀分布的垃圾箱,以用于汇总表的值。 我的间隔是:
(MAX(date) - MIN(date)) / bins
这里date是一个表的列 并且 bins是并列我想表分成数。
date
bins
date_0= MIN(日期) date_n= MIN(日期)+(间隔* n)
date_0
date_n
我建议使用方便的功能 width_bucket() :
width_bucket()
要获取每个时间段(“ bin”)的平均值:
SELECT width_bucket(extract(epoch FROM t.the_date) , x.min_epoch, x.max_epoch, x.bins) AS bin , avg(value) AS bin_avg FROM tbl t , (SELECT extract(epoch FROM min(the_date)) AS min_epoch , extract(epoch FROM max(the_date)) AS max_epoch , 10 AS bins FROM tbl t ) x GROUP BY 1;
要获得(逐步)增长时间间隔内的“运行平均值”:
SELECT bin, round(sum(bin_sum) OVER w /sum(bin_ct) OVER w, 2) AS running_avg FROM ( SELECT width_bucket(extract(epoch FROM t.the_date) , x.min_epoch, x.max_epoch, x.bins) AS bin , sum(value) AS bin_sum , count(*) AS bin_ct FROM tbl t , (SELECT extract(epoch FROM min(the_date)) AS min_epoch , extract(epoch FROM max(the_date)) AS max_epoch , 10 AS bins FROM tbl t ) x GROUP BY 1 ) sub WINDOW w AS (ORDER BY bin) ORDER BY 1;
使用the_date而不是date作为列名,避免使用保留字作为标识符。 由于width_bucket()目前仅针对doubleprecision和实现numeric,因此我从中提取历元值the_date。
the_date
doubleprecision
numeric