简体   繁体   English

使用date_trunc的滚动平均值

[英]Rolling average using date_trunc

I'm using date_trunc to aggregate over a timestamp field. 我正在使用date_trunc汇总时间戳字段。 I'm trying to count the number of unique values in a given field, over a month. 我试图计算一个月内给定字段中唯一值的数量。 If I choose month as my resolution : 如果我选择month作为解决方案:

SELECT date_trunc('month', timestamp), COUNT(DISTINCT(foo)) FROM ...

then all resulting entries are at the beginning of each month, and so “2017-01-01" would be counting the unique entries where the timestamp field is anywhere in January. 那么所有产生的条目都在每个月初,因此“ 2017-01-01”将计算timestamp字段在1月中任意位置的唯一条目。

Is there a way to specify an offset such that I might have an entry at 2017-01-15, comprising entries up to 2017-02-15 ? 有没有一种方法可以指定一个偏移量,以便我可能在2017年1月15日有一个条目,包括直到2017年2月15日的条目?

Thanks. 谢谢。

SELECT 
    date_trunc('month', timestamp + interval '15 day'),
    COUNT(DISTINCT(foo)) 
FROM ...
group by 1

This kind of queries are never very efficient. 这种查询永远不会非常有效。 If speed is an issue, perhaps it would be better to do this outside of the database through looping in the application language 如果速度是一个问题,那么最好通过在应用程序语言中循环在数据库之外执行此操作

In the subquery, for every record, we generate a series of dates which will include it in their aggregations, then we aggregate by the generated dates. 在子查询中,对于每条记录,我们都会生成一系列日期,这些日期将其包含在它们的聚合中,然后根据生成的日期进行聚合。

SELECT
    mydate
  , COUNT(DISTINCT foo)
FROM (SELECT GENERATE_SERIES( DATE("timestamp") - INTERVAL '30 DAYS'
                            , DATE("timestamp"), '1 DAY') mydate
           , foo
      FROM mytable) expanded
GROUP BY 1

Also, please try NOT to use timestamp as a column name as its a postgresql data type. 另外,请尽量不要将timestamp用作列名作为postgresql数据类型。

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

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