简体   繁体   English

以 8 小时的间隔获取时间序列

[英]get time series in 8 hours of interval

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

SELECT * from (
    select * from generate_series(
        date_trunc('hour', '2021-11-13 10:01:38'::timestamp),
        '2021-12-13 10:01:38'::timestamp,
        concat(480, ' minutes')::interval) as t(time_ent)) as t
    where t."time_ent" between '2021-11-13 10:01:38'::timestamp and '2021-12-13 10:01:38'::timestamp

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

2021-11-13 18:00:00.000
2021-11-14 02:00:00.000
2021-11-14 10:00:00.000
2021-11-14 18:00:00.000
2021-11-15 02:00:00.000

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

2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000

currently, the time series hours depend upon the timestamp that I pass.目前,时间序列小时数取决于我通过的时间戳。 in above it gives me hours like 02,10,18...but I want the hours like 00,08,16...hours should not depend on the time I passed in query.在上面它给了我像02,10,18这样的小时......但我想要像00,08,16这样的小时......小时不应该取决于我通过查询的时间。 I tried many things but not any success.我尝试了很多东西,但没有任何成功。

as your start of generate_series is set to 10:00:00, so your next step will be 18:00:00 you have to start your serie from 00:00:00 (cast to date) eg:由于您的 generate_series 开始时间设置为 10:00:00,因此您的下一步将是 18:00:00,您必须从 00:00:00(迄今为止)开始您的系列赛,例如:

SELECT 
time_ent::timestamp without time zone
from (
    select * from generate_series(
        date_trunc('hour', '2021-11-13 10:01:38'::date),
        '2021-12-13 10:01:38'::timestamp ,
        concat(480, ' minutes')::interval) as t(time_ent)
        ) as t
    where t."time_ent" between '2021-11-13 10:01:38'::timestamp   and '2021-12-13 10:01:38'::timestamp 
    

and the result will be:结果将是:

2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000
2021-11-15 08:00:00.000

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

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