现在我尝试解决一个问题,我的理解似乎与在巨大的事件流中寻找差距有关?
我的表中有几个数据流。我想按时间总结它们,但它们并不总是相同的时间戳。该表如下所示:
架构:
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。
我考虑过递归查询,但不知何故我看不到解决方案......
编辑:在这里我试图以图形方式解释它:
编辑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;