Search code examples
mysqlsqltimequery-optimizationrecursive-query

Generate time serie between time interval with steps of minutes on the fly in SQL


The question is in the end of this post.

I found this link to generate date Generating a series of dates and modified it to generate time in an intervall:

SET @start_time = "08:02";
SET @stop_time  = "17:02";
SELECT
     TIME_FORMAT(time(CONCAT(m3, m2, ':', m1, m0)) , "%H:%i") as Time
FROM
    (SELECT 0 m0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS m0,
    (SELECT 0 m1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) AS m1,
    (SELECT 0 m2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS m2,
    (SELECT 0 m3 UNION SELECT 1 UNION SELECT 2) AS m3
where time(CONCAT(m3, m2, ':', m1, m0)) is not null 
and time(CONCAT(m3, m2, ':', m1, m0)) >= @start_time
and time(CONCAT(m3, m2, ':', m1, m0)) <= @stop_time
order by Time asc

This will generate this table:

Time
10:02
10:03
10:04
10:05
10:06
...
16:59
17:00
17:01

Question?

Can this be written in a more effective way? I should like to specify an interval with steps, so can quickly update it to every X minute intervall instead. I don't want to store the result in DB, i just want to generate it on the fly. Since I will join it with other tables.

I use this a lot in mysql queries to make time serie for measurements that are easy to plot in Excel.


Solution

  • I'm a fan of recursive CTEs for this purpose:

    with recursive times as (
          select time('10:00:00') as time
          union all
          select time + interval 17 minute
          from times
          where time < time('17:00:00')
         )
    select *
    from times;
    

    Here is a db<>fiddle.