Skip to content
Advertisement

insert extra rows in query result sql

Given a table with entries at irregular time stamps, “breaks” must be inserted at regular 5 min intervals ( the data associated can / will be NULL ).

I was thinking of getting the start time, making a subquery that has a window function and adds 5 min intervals to the start time – but I only could think of using row_number to increment the values.

However this limits me to the number of rows present for an id in the original data table, and if the timestamps are spread out, the number of rows cannot cover the amount of 5 min intervals needed to be added.

sample data:

I was thinking of creating a temporary table inside HIVE and filling it with x rows representing 5 min intervals from the starttime to the endtime of the data table, but I couldn’t find any way of accomplishing that.

Any way of using “for loops” ? Any suggestions would be appreciated.

Thanks

Advertisement

Answer

You can try calculating the difference between current timestamp and next one, divide 300 to get number of ranges, produce a string of spaces with length = num_ranges, explode to generate rows.

Demo:

Result:

Additional rows were added. I left all intermediate columns for debugging purposes.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement