简体   繁体   English

以 10 分钟的间隔获取时间序列

[英]get time series in 10 minutes of interval

I am generating one time-series from using below query.我正在使用以下查询生成一个时间序列。

SELECT date_trunc('min', dd):: TIMESTAMP WITHOUT TIME zone as time_ent 
FROM generate_series ( timestamp '2021-12-09 06:34:37' + ((DATE_PART('min', timestamp '2021-12-09 06:34:37')::integer % 2) || ' minutes') :: INTERVAL 
, '2021-12-10 06:34:37'::timestamp 
, '20 min'::interval) dd

and it will give me output like below.它会给我 output 如下所示。

2021-12-09 06:34:00.000
2021-12-09 06:54:00.000
2021-12-09 07:14:00.000
2021-12-09 07:34:00.000

but I need output like.但我需要 output 之类的。

2021-12-09 06:40:00.000
2021-12-09 07:00:00.000
2021-12-09 07:20:00.000
2021-12-09 07:40:00.000

currently, the time series hours depend upon the timestamp that I pass.目前,时间序列小时数取决于我通过的时间戳。 in above it gives me mins like 34,54,14...but I want the mins like 40,00,20...it should not depend on the time I passed in query.在上面它给了我像 34,54,14 这样的分钟......但我想要像 40,00,20 这样的分钟......它不应该取决于我通过查询的时间。 I tried with timestamp '2021-12-09 06:34:37' + ((DATE_PART('min', timestamp '2021-12-09 06:34:37')::integer % 2) || ' minutes'):: INTERVAL but not any success.我尝试使用时间戳 '2021-12-09 06:34:37' + ((DATE_PART('min', timestamp '2021-12-09 06:34:37')::integer % 2) || '分钟' ):: INTERVAL但没有任何成功。

Based on your description, I assumed that you want to create a series of timestamps for 00, 20, 40 minute at each hour until the next day.根据您的描述,我假设您要在每个小时创建一系列 00、20、40 分钟的时间戳,直到第二天。

select * 
from (
    select date_trunc('hour', current_timestamp) + i * interval '20 minutes' as ts
    from generate_series(1, 24*3) as t(i)) t
where ts between current_timestamp and current_timestamp + interval '1 day'

The key idea here is to truncate the current_timestamp to 00 minute first.这里的关键思想是首先将current_timestamp截断为00 minute This becomes the start of the series.这成为系列的开始。 Then filter out the generated timestamps outside the range you want.然后过滤掉你想要的范围之外的生成时间戳。 You may need to adjust the second argument of generate_series function, depending on your requirement.您可能需要根据您的要求调整generate_series function 的第二个参数。

Or you can just generate a series of timestamp like the following:或者您可以生成一系列时间戳,如下所示:

select *
from (
    select ts
    from generate_series(
      date_trunc('hour', current_timestamp),
      current_timestamp + interval '1 day',
      interval '20 minutes') as t(ts)) as t
where ts between current_timestamp and current_timestamp + interval '1 day'

Here, you still need to trunc your timestamp to hour first so the start of the series at 00 minute.在这里,您仍然需要先将时间戳截断为小时,以便系列从 00 分钟开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM