我有一个查询,该查询会生成一个时间戳和该时间戳的值。
select timestamp, value
from table1;
因此,数据样本如下:
timestamp | value |
---------------------------------------
2019-02-28 01:00:00 | 1 |
2019-02-28 01:10:00 | 1 |
2019-02-28 01:20:00 | 6 |
2019-02-28 01:30:00 | 17 |
2019-02-28 01:40:00 | 4 |
2019-02-28 01:50:00 | 4 |
我有第二个查询,它生成开始时间戳和结束时间戳。
select start, end
from table2;
其中的一个示例如下所示:
start | end |
-------------------------------------------
2019-02-28 01:00:00 | 2019-02-28 01:30:00 |
2019-02-28 01:04:00 | 2019-02-28 01:50:00 |
如何将两个查询结合起来以产生这样的输出?
start | end | values |
-------------------------------------------------------------
2019-02-28 01:00:00 | 2019-02-28 01:30:00 | {1,1,6} |
2019-02-28 01:40:00 | 2019-02-28 01:50:00 | {4} |
答案 0 :(得分:0)
汇总和join
:
select t2.start, t2.end, array_agg(t1.value)
from table2 t2 left join
table1 t1
on t1.timestamp >= t2.start and t1.timestamp < t2.end
group by t2.start, t2.end;