总结来自不同流的图表,但时间戳略有不同

时间:2015-06-03 13:27:03

标签: postgresql postgresql-9.4

现在我尝试解决一个似乎与我的理解有关的问题Finding gaps in huge event streams?

我的表中有几个数据流。我想按时间总结它们,但它们并不总是相同的时间戳。该表如下所示:

架构:

CREATE TABLE Table1
    ("id" int, "stream_id" int, "timestamp" timestamp, "value" real)
;

INSERT INTO Table1
    ("id", "stream_id", "timestamp", "value")
VALUES
    (1, 7, '2015-06-01 15:20:30', 0.1),
    (2, 7, '2015-06-01 15:20:31', 0.2),
    (3, 7, '2015-06-01 15:20:32', 0.3),
    (4, 7, '2015-06-01 15:25:30', 0.5),
    (5, 7, '2015-06-01 15:25:31', 1.0),

    (6, 6, '2015-06-01 15:20:31', 1.1),
    (7, 6, '2015-06-01 15:20:32', 1.2),
    (8, 6, '2015-06-01 15:20:33', 1.3),
    (9, 6, '2015-06-01 15:25:31', 1.5),
    (10, 6, '2015-06-01 15:25:32', 2.0)    
;

我试图解决它:

with ts as (select "timestamp"       
           from Table1
           order by "timestamp"
          ),
data as (select "timestamp","value"
         from Table1
         order by "timestamp"
         ),
streams as (select "stream_id"        
           from Table1
           group by "stream_id"
           order by "stream_id"
          )          
select * .... (question)

我希望获得所有汇总数据的图表行。当一次,其他流中没有数据时,总和应该采用timestamp < current_timestamp但最接近当前time_stamp的行。如果没有值,则假设为0.

我考虑过递归查询,但我不知道怎么看不到解决方案......

编辑:我试图以图形方式解释它:

graphical description of my problem

编辑2:

我想到这样的事情,但我没有得到完成它的最后“东西”。

with RECURSIVE data as (
    select * from rawdata 
    where date(date_time)='2014-05-01'
),
streams as (
    select stream_id from data 
    group by stream_id
),
t(n) AS (
    VALUES (1)
  UNION ALL
    SELECT n+1 FROM t WHERE n < (select count(*) from streams)
)
SELECT n FROM t;

1 个答案:

答案 0 :(得分:1)

我道歉,上一次查询中有错误 这是一个新的,更正的查询:

WITH times AS(
  SELECT DISTINCT "timestamp" As tm
  FROM table1
)
SELECT tm, SUM( val ) as s_u_m
FROM (
    SELECT tm, "stream_id",
           (  SELECT "value" FROM Table1 t2
              WHERE t2."timestamp" = max( t1."timestamp" )
                AND t2."stream_id" = t1."stream_id"
              ORDER BY "id" DESC LIMIT 1
            ) As val
    FROM times t
    JOIN table1 t1
    ON t.tm >= t1."timestamp"
    GROUP BY tm, "stream_id"
    order by tm
  ) you_must_have_an_alias_here_in_order_to_avoid_the_syntax_error
GROUP BY tm
ORDER BY tm;
;

以及源数据中包含3个流的演示:http://sqlfiddle.com/#!15/30eb8/5

这是一个源表,其布局模仿图表的布局:

|  x | id |              timestamp | stream6 | stream7 | stream8 |
|----|----|------------------------|---------|---------|---------|
|  1 |  1 | June, 01 2015 15:20:30 |  (null) |     0.1 |  (null) |
|  2 |  2 | June, 01 2015 15:20:31 |  (null) |     0.2 |  (null) |
|  3 |  3 | June, 01 2015 15:20:31 |     1.1 |  (null) |  (null) |
|  4 |  4 | June, 01 2015 15:20:32 |  (null) |     0.3 |  (null) |
|  5 |  5 | June, 01 2015 15:20:32 |     1.2 |  (null) |  (null) |
|  6 | 11 | June, 01 2015 15:20:32 |  (null) |  (null) |     2.3 |
|  7 | 12 | June, 01 2015 15:20:32 |  (null) |  (null) |     1.1 |
|  8 | 10 | June, 01 2015 15:20:33 |     1.3 |  (null) |  (null) |
|  9 | 13 | June, 01 2015 15:20:33 |  (null) |  (null) |     1.7 |
| 10 |  6 | June, 01 2015 15:25:30 |  (null) |     0.5 |  (null) |
| 11 |  7 | June, 01 2015 15:25:31 |     1.5 |  (null) |  (null) |
| 12 |  8 | June, 01 2015 15:25:31 |  (null) |       1 |  (null) |
| 13 |  9 | June, 01 2015 15:25:32 |       2 |  (null) |  (null) |

结果是:(v(3)表示:x = 3的记录中的值)

|                     tm |     s_u_m |
|------------------------|-----------|
| June, 01 2015 15:20:30 |       0.1 |  0    + v(1) + 0
| June, 01 2015 15:20:31 | 1.3000001 |  v(3) + v(2)  + 0
| June, 01 2015 15:20:32 |       2.6 |  v(5) + v(4) + v(7) => see note below !!!
| June, 01 2015 15:20:33 |       3.3 |  v(8) + v(4) + v(9)
| June, 01 2015 15:25:30 |       3.5 |  v(8) + v(10)+ v(9)
| June, 01 2015 15:25:31 |       4.2 |  v(11)+ v(12)+ v(9)
| June, 01 2015 15:25:32 |       4.7 |  v(13)+ v(12)+ v(9)

请注意记录 | June, 01 2015 15:20:32 | 2.6 |

演示中的源表包含两个具有相同日期和相同source_id的记录:

|  6 | 11 | June, 01 2015 15:20:32 |  (null) |  (null) |     2.3 |
|  7 | 12 | June, 01 2015 15:20:32 |  (null) |  (null) |     1.1 |

由于此代码片段中的ORDER BY "id" DESC,查询仅选取最新记录x = 7:

(  SELECT "value" FROM Table1 t2
      WHERE t2."timestamp" = max( t1."timestamp" )
        AND t2."stream_id" = t1."stream_id"
      ORDER BY "id" DESC LIMIT 1
    ) As val

如果您想要获取第一条记录x = 6而不是最新记录,请从order by子句中删除DESC

如果要对具有相同日期和stream_id的所有记录求和(在上面的示例中 - 记录6 + 7),则将上述查询更改为:

(  SELECT SUM("value") FROM Table1 t2
      WHERE t2."timestamp" = max( t1."timestamp" )
        AND t2."stream_id" = t1."stream_id"
    ) As val

如果您想获取随机记录,请使用ORDER BY random()