1

在下面的查询中,我正在使用 PostgreSQL 计算两小时间隔的最小值、最大值和平均值。

该查询在偶数开始时间可以正常工作(..04:00:00+05:30),但它给出的结果与奇数开始时间的偶数开始时间相似(..05:00:00+05:30)。乘以 2 的倍数返回偶数小时,这就是问题所在。

SELECT tagid, CAST(sample_time_stamp as Date) AS stat_date, 
       (floor(extract(hour from sample_time_stamp)/2) * 2)::int AS hrs,
       min(sensor_reading) AS theMin,   
       max(sensor_reading) AS theMax,
       avg(sensor_reading) AS theAvg
FROM sensor_readings WHERE tagid =1 AND 
sample_time_stamp BETWEEN '2012-10-23 01:00:00+05:30'
                  AND     '2012-10-23 05:59:00+05:30'
GROUP BY tagid,CAST(sample_time_stamp as Date),
         floor(extract(hour from sample_time_stamp)/2) * 2
ORDER BY tagid,stat_date, hrs

奇数开始时间的输出 ('2012-10-23 01:00:00+05:30')

tagid    date          hrs  theMin  themax    theAvg 
1        2012-10-23    0    6       58        30.95
1        2012-10-23    2    2       59        29.6916666666667
1        2012-10-23    4    3       89        31.7666666666667

偶数开始时间的输出 ('2012-10-23 02:00:00+05:30')

tagid    date          hrs  theMin   themax    theAvg
1        2012-10-23    2    2        59        29.6916666666667
1        2012-10-23    4    3        89        31.7666666666667
4

1 回答 1

3

要从您的最小时间戳开始获得恒定的时间范围:

WITH params AS (
   SELECT '2012-10-23 01:00:00+05:30'::timestamptz AS _min  -- input params
         ,'2012-10-23 05:59:00+05:30'::timestamptz AS _max
         ,'2 hours'::interval                      AS _interval
   )
  ,ts AS (SELECT generate_series(_min, _max, _interval) AS t_min FROM params)
  ,timeframe AS (
   SELECT t_min
         ,lead(t_min, 1, _max) OVER (ORDER BY t_min) AS t_max
   FROM ts, params
   )
SELECT s.tagid
      ,t.t_min
      ,t.t_max     -- mildly redundant except for last row
      ,min(s.sensor_reading) AS the_min
      ,max(s.sensor_reading) AS the_max
      ,avg(s.sensor_reading) AS the_avg
FROM   timeframe t
LEFT   JOIN sensor_readings s ON  s.tagid = 1
                              AND s.sample_time_stamp >= t.t_min
                              AND s.sample_time_stamp <  t.t_max
GROUP  BY 1,2,3
ORDER  BY 1,2;

可用于任何时间范围和任何间隔长度。需要 PostgreSQL 8.4 或更高版本。

如果最大时间戳_max不落在_min + n * _interval最后一个时间帧上,则截断。因此,最后一行可以表示比您想要的更短的时间范围_interval

关键要素

使用递归 CTE 生成替代时间框架:

WITH RECURSIVE params AS (
   SELECT '2012-10-23 01:00:00+05:30'::timestamptz AS _min  -- input params
         ,'2012-10-23 05:59:00+05:30'::timestamptz AS _max
         ,'2 hours'::interval                      AS _interval
   )
   , timeframe AS (
   SELECT _min AS t_min, LEAST(_min + _interval, _max) AS t_max
   FROM   params

   UNION  ALL
   SELECT t_max, LEAST(t_max + _interval, _max)
   FROM   timeframe t, params p
   WHERE  t_max < _max
   )
SELECT ...

稍微快一点……随你挑。
-> sqlfiddle显示两者。

请注意,即使在声明时也可以拥有非递归WITH RECURSIVECTE(另外) 。

表现与指数

应该比您的原始查询更快。一半的代码处理生成时间栅格,这涉及很少的行并且非常快。处理实际表行(昂贵的部分)变得更便宜,因为我们不再计算新值sample_time_stamp

你绝对应该有一个形式的多列索引

CREATE INDEX foo_idx ON sensor_readings (tagid, sample_time_stamp DESC);

DESC假设您更频繁地查询最近的条目(后来的时间戳)。如果不是这种情况,请删除修饰符。无论哪种方式都没有太大的区别。

于 2012-11-02T21:05:31.883 回答