I have the sequence of numbers below and I want to make a new group of numbers every time the following number has a difference of more than one unit from the previous value, i.e. breaking the continuous interval
Table T
value
1
2
3
5
6
7
15
16
17
18
Groups to be formed of continuous interval:
min max
1 3
5 7
15 18
I am trying to number the rows using dense_rank() function, like the example below, then i will be able to group by rankNumber and get the MIN(value) and MAX(value), but i am not finding a pattern to use in the PARTITION BY clause of this function
value rankNumber
1 1
2 1
3 1
5 2
6 2
7 2
15 3
16 3
17 3
18 3
WITH T2 AS
(
SELECT value, LEAD(value) OVER(ORDER BY value) as nextValue
FROM T
)
SELECT value, DENSE_RANK()
OVER(PARTITION BY CASE WHEN nextValue - value > 1 THEN 1 ELSE 0 END ORDER BY value)
FROM T2
Code to create table:
CREATE TABLE t(
value INT
);
INSERT INTO t VALUES
(1), (2), (3), (5), (6), (7), (15), (16), (17), (18);
Current output using the query above:
value rankNumber
1 1
2 2
3 1
5 3
6 4
7 2
15 5
16 6
17 7
18 8