Skip to content
Advertisement

Split row data base on timestamp SQL Oracle

Good day everyone. I have a table as below. Duration is the time from current state to next state.

Timestamp State Duration(minutes)
10/9/2022 8:50:00 AM A 35
10/9/2022 9:25:00 AM B 10
10/9/2022 9:35:00 AM C

How do I split data at 9:00 AM of each day like below:

Timestamp State Duration(minutes)
10/9/2022 8:50:00 AM A 10
10/9/2022 9:00:00 AM A 25
10/9/2022 9:25:00 AM B 10
10/9/2022 9:35:00 AM C

Thank you.

Advertisement

Answer

Use a row-generator function to generate extra rows when the timestamp is before 09:00 and the next timestamp is after 09:00 (and calculate the diff value rather than storing it in the table):

Which, for your sample data:

Outputs:

TIMESTAMP STATE DIFF
2022-10-09 08:50:00 A 10
2022-10-09 09:00:00 A 25
2022-10-09 09:25:00 B 10
2022-10-09 09:35:00 C 1405
2022-10-10 09:00:00 C 1440
2022-10-11 09:00:00 C 1440
2022-10-12 09:00:00 C 35
2022-10-12 09:35:00 D null

fiddle

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