PostgreSQL中的时间聚合

时间:2011-11-07 13:54:42

标签: postgresql aggregate-functions aggregation temporal-database

我正在使用PostgreSQL数据库进行时间聚合的Java实现。

我的表格看起来像这样

Value | Start      | Stop
(int) | (Date)     | (Date)
-------------------------------
1     | 2004-01-01 | 2010-01-01
4     | 2000-01-01 | 2008-01-01

所以想象一下这个时期:

                      ------------------------------
  ----------------------------------------
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
  [        4         ][       5=4+1      ][    1   ]

我的算法现在计算数据的时间聚合,例如SUM():

Value | Start      | Stop
-------------------------------
4     | 2000-01-01 | 2004-01-01
5     | 2004-01-01 | 2008-01-01
1     | 2008-01-01 | 2010-01-01

为了测试获得的结果,我现在想使用PostgreSQL直接查询数据。我知道这个问题还没有简单的方法。但是,肯定有办法获得相同的结果。应支持聚合计数,最大值,最小值,总和和平均值。我不介意一个坏的或缓慢的解决方案,只需要工作。

到目前为止我发现的查询应该具有类似的作用如下:

select count(*), ts, te
from ( checkout a normalize checkout b using() ) checkoutNorm
group by ts, te;

我的收养看起来像这样:

select count(*), start, stop
from ( myTable a normalize myTable b using() ) myTableNorm
group by start, stop;

但是,报告了错误ERROR: syntax error at or near "normalize" -- LINE 2: from ( ndbs_10 a normalize ndbs_10 b using() ) ndbsNorm

有没有人能解决这个问题?它不必基于上述查询,只要它有效。非常感谢。

1 个答案:

答案 0 :(得分:2)

你的问题很难理解。但我我想出来了。
您想要一个超过value的运行总和。值仅适用于某个时间段的startstop之间。因此,必须在该期间开始时添加它们并在结束时扣除 此外,您希望结果期间的开始和结束总和有效。
应该这样做:

-- DROP SCHEMA x CASCADE;
CREATE SCHEMA x;
CREATE TABLE x.tbl(val int, start date, stop date);
INSERT INTO x.tbl VALUES
 (4 ,'2000-01-01' ,'2008-01-01')
,(7 ,'2001-01-01' ,'2009-01-01')
,(1 ,'2004-01-01' ,'2010-01-01')
,(2 ,'2005-01-01' ,'2006-01-01');

WITH a AS (
    SELECT start as ts, val FROM x.tbl
    UNION  ALL
    SELECT stop, val * (-1) FROM x.tbl
    ORDER  BY 1, 2)
SELECT sum(val) OVER w AS val_sum
      ,ts AS start
      ,lead(ts) OVER w AS stop
FROM   a
WINDOW w AS (ORDER BY ts)
ORDER  BY ts;

val_sum |   start    |    stop
--------+------------+------------
      4 | 2000-01-01 | 2001-01-01
     11 | 2001-01-01 | 2004-01-01
     12 | 2004-01-01 | 2005-01-01
     14 | 2005-01-01 | 2006-01-01
     12 | 2006-01-01 | 2008-01-01
      8 | 2008-01-01 | 2009-01-01
      1 | 2009-01-01 | 2010-01-01
      0 | 2010-01-01 |

请求后编辑

对于所有请求的聚合函数:

SELECT period
      ,val_sum
      ,val_count
      ,val_sum::float /val_count AS val_avg
      ,(SELECT min(val) FROM x.tbl WHERE start < y.stop AND stop > y.start) AS val_min
      ,(SELECT max(val) FROM x.tbl WHERE start < y.stop AND stop > y.start) AS val_max
      ,start
      ,stop
FROM   (
    WITH a AS (
         SELECT start as ts, val, 1 AS c FROM x.tbl
         UNION  ALL
         SELECT stop, val, -1 FROM x.tbl
         ORDER  BY 1, 2)
    SELECT count(*) OVER w AS period
          ,sum(val*c) OVER w AS val_sum
          ,sum(c) OVER w AS val_count
          ,ts AS start
          ,lead(ts) OVER w AS stop
    FROM   a
    WINDOW w AS (ORDER BY ts)
    ORDER  BY ts
    ) y
WHERE stop IS NOT NULL;

 period | val_sum | val_count | val_avg | val_min | val_max |   start    |    stop
--------+---------+-----------+---------+---------+---------+------------+------------
      1 |       4 |         1 |       4 |       4 |       4 | 2000-01-01 | 2001-01-01
      2 |      11 |         2 |     5.5 |       4 |       7 | 2001-01-01 | 2004-01-01
      3 |      12 |         3 |       4 |       1 |       7 | 2004-01-01 | 2005-01-01
      4 |      14 |         4 |     3.5 |       1 |       7 | 2005-01-01 | 2006-01-01
      5 |      12 |         3 |       4 |       1 |       7 | 2006-01-01 | 2008-01-01
      6 |       8 |         2 |       4 |       1 |       7 | 2008-01-01 | 2009-01-01
      7 |       1 |         1 |       1 |       1 |       1 | 2009-01-01 | 2010-01-01

min()max可能会进行优化,但这应该足够好了。 正如您所见,CTE(WITH子句)和子查询是可交换的。