0

Please help with the request, there is such data: you need to break this data into intervals, in the status column, so that approximately it looks like this:

data_image_example

client| date_start        | date_end          | status
-------------------------------------------------------
1     | 01.06.2019 0:00:00| 01.06.2019 0:02:00| 2
1     | 01.06.2019 0:02:00| 01.06.2019 0:04:00| 1
1     | 01.06.2019 0:04:00| 01.06.2019 0:05:00| 2
0

1 Answer 1

1

This is a form of gaps-and-islands with a twist. You can identify the adjacent rows using a difference of row numbers. Then aggregate and use lead() to get the end time:

select client, status, min(time) as starttime,
       lead(min(time)) over (partition by client order by min(time)) as endtime
from  (select t.*,
              row_number() over (partition by client order by time) as seqnum,
              row_number() over (partition by client, status order by time) as seqnum_s
       from t
      ) t
group by (seqnum - seqnum_s), status, client;
Sign up to request clarification or add additional context in comments.

2 Comments

if I have a fixed interval of 1 minute, and suddenly a segment break, how can I take this into mind?
@ivani . . . I would suggest asking a new question with appropriate sample data and desired results. Please add the data as text tables in the question. A db fiddle of some sort is also helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.