简体   繁体   中英

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.

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.

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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