为一列的每个不同ID计算缺少的时间戳

时间:2019-12-05 16:01:11

标签: sql time-series vertica

我有一个Vertica数据库,我们在其中存储时间序列数据。每个时间序列都对应一个item_id,看起来像这样

start_date,item_id,value
2019-10-01 16:15:00.000000,466808277,16
2019-10-01 16:15:00.000000,466808289,23
2019-10-01 16:15:00.000000,466808293,18
2019-10-01 16:15:00.000000,466808312,85
2019-10-01 16:15:00.000000,466808316,19
2019-10-01 16:15:00.000000,466808367,29
2019-10-01 16:15:00.000000,466808383,24
2019-10-01 16:15:00.000000,466808446,106

时间戳以15分钟为单位(例如2019-10-01 16:15,2019-10-01 16:30,2019-10-01 16:45)。我想计算每个item_id有多少个缺少的时间戳。

尽管我创建了一个临时表,其时间戳从最小的start_date到最大的start_date,然后将它们加入以检查缺少的时间戳。

SELECT ts
FROM
(
    SELECT '2018-09-14 08:00:00'::TIMESTAMP as tm
    UNION
    SELECT '2019-10-09 17:00:00'::TIMESTAMP as tm
) as t
TIMESERIES ts as '15 minutes' OVER (ORDER BY tm)

连接的问题是,如果有多个项目缺少时间戳,那么由于我们正在与临时表(仅具有连续的时间戳)进行连接,因此它将仅显示一个空条目。看起来像这样

select ts, item_id, start_date, speed from public.measure_link_aforos_inrix_15min as links right outer join
(SELECT ts
FROM
(
    SELECT '2018-09-14 08:00:00'::TIMESTAMP as tm
    UNION
    SELECT '2019-10-09 17:00:00'::TIMESTAMP as tm
) as t
TIMESERIES ts as '15 minutes' OVER (ORDER BY tm)) as calendar on links.start_date = calendar.ts
where start_date is null

如何获得在时间序列方面也考虑item_id维的联接? (ts表)

2 个答案:

答案 0 :(得分:0)

我认为您希望cross join生成行,然后希望left join引入值。我不能轻易跟踪您的表名,但是像这样:

select ts.timestamp, i.item_id, t.value
from (<your query here for ts>) ts cross join
     (select distinct item_id from timeseries) i left join
     timeseries t
     on t.timestamp = ts.timestamp and t.item_id = i.item_id

答案 1 :(得分:0)

答案取决于您要计算每个this.contactFields$.pipe(map(x => transformArr(x))); // ...later when you subscribe this.contactFields$.subscribe(x => console.log(x)); 的预期行数还是整个时间序列的预期总行数。

item_id