总结来自不同流但时间戳略有不同的图表
sum up graphs from different streams but slightly different timestamps
现在我试图解决一个问题,据我所知似乎与
有关
我的 table 中有多个数据流。我想按时间总结它们,但它们并不总是相同的时间戳。 table 看起来像这样:
架构:
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:
我在想类似的事情,但最后"thingy"我没有完成它。
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;
抱歉,之前的查询有误
这是一个新的、更正的查询:
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
这是一个来源table,其布局模仿您的图表布局:
| 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 |
演示中的来源table包含两条日期相同且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()
。
现在我试图解决一个问题,据我所知似乎与
我的 table 中有多个数据流。我想按时间总结它们,但它们并不总是相同的时间戳。 table 看起来像这样:
架构:
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:
我在想类似的事情,但最后"thingy"我没有完成它。
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;
抱歉,之前的查询有误
这是一个新的、更正的查询:
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
这是一个来源table,其布局模仿您的图表布局:
| 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 |
演示中的来源table包含两条日期相同且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()
。