SQL - Fill timestamp gaps and split rows to monitor complete days

25 Views Asked by At

I have the following data set in a SQL DB:

enter image description here

And that's what I want to achieve:

enter image description here

This results in 4 tasks:

1. if an entry extends over one day, an entry should be set in the endTimestamp at 23:59:59 and on the following day a row that starts at 00:00:00 and ends with the original endTimestamp.

2. if there is a time gap between two statuses (StatusID), then this should be filled with the StatusID = 0.

3. if the status ends before 23:59:59 on one day, the remaining day up to 23:59:59 should be filled with status = 0.

4. if there are no entries for several days, these should be filled accordingly.

I would be very grateful if you could help me. I just can't find the correct solution.

I tried something using Datediff and Datetime2fromparts, but it doesn't really work.

0

There are 0 best solutions below