Search code examples
sqlsql-servertimestampresolutionintervals

How to convert 5min timestamp intervals to 1min intervals in SQL keeping the other information?


I saw one or two topics asking about the opposite (turning 1min timestamp into 5min or other timestamp intervals). But I quite do not get the idea of programming this.

This is my input table:

timestamp     Food      Beverage
15:00:00      Pizza      Coke
15:05:00      Burger     Coke
15:10:00      Hotdog     Pepsi
   …             …        …

And this would be the output I want:

timestamp   Food    Beverage
 15:00:00   Pizza   Coke
 15:01:00   Pizza   Coke
 15:02:00   Pizza   Coke
 15:03:00   Pizza   Coke
 15:04:00   Pizza   Coke
 15:05:00   Burger  Coke
 15:06:00   Burger  Coke
 15:07:00   Burger  Coke
 15:08:00   Burger  Coke
 15:09:00   Burger  Coke
 15:10:00   Hotdog  Pepsi
 15:11:00   Hotdog  Pepsi
    …         …       …

Thanks in advance!


Solution

  • The simplest way is to use cross join:

    select dateadd(minute, v.m, timestamp) as timestamp,
           food, beverage
    from t cross join
         (values (0), (1), (2), (3), (4)) v(m);
    

    You can also do this with an explicit select/union all:

    select dateadd(minute, v.m, timestamp) as timestamp,
           food, beverage
    from t cross join
         (select 0 as m union all select 1 union all select 2 union all select 3 union all select 4
         ) v(m);