How to count groups for a sequence SQL
- bdata3
- Dec 22, 2019
- 1 min read
Updated: Dec 23, 2019
Look on the following Oracle article - how to create groups for sequence records using SQL window functions :
select val
,row_number() over (order by val) as rn
,val-row_number() over (order by val) as grp
from
(values (1,2,3),(2,2,3),(4,2,3),(9,2,3),(10,2,3),(15,2,3)) t1 (val,a,b)
order by val
and the result:
|val |rn |grp |
|-----------|-----------------|------------------|
|1 |1 |0 |
|2 |2 |0 |
|4 |3 |1 |
|9 |4 |5 |
|10 |5 |5 |
|15 |6 |9 |
Comments