Oracle - grouping values along interval Oracle - grouping values along interval oracle oracle

Oracle - grouping values along interval


select      min (step)                                                   as "from"           ,nullif (max (step),max(min(step)) over (partition by data))  as "to"           ,datafrom       (select      step,data                       ,row_number () over (partition by data order by step) as n            from        t            ) group by    data           ,step - n            order by    "from"           


You can do this by generating a sequence of numbers and subtracting from the step. This will be a constant when the values are sequential:

select min(step) as from_step, max(step) as to_step, datafrom (select t.*,            row_number() over (partition by data order by step) as seqnum     from t    ) tgroup by (step - seqnum), data;

EDIT:

It is not quite clear where the NULLs are coming from. If I speculate that they are the last values for each value, you can do:

select min(step) as from_step,       (case when max(step) <> max_step then max(step) end) as to_step,       datafrom (select t.*,             max(step) over (partition by data) as max_step             row_number() over (partition by data order by step) as seqnum     from t    ) tgroup by (step - seqnum), data, max_step;