如何对数字列在特定范围内的组进行分组然后计数?

How to group then count groups where a numeric column is within a particular range?

基本上我想计算列在特定范围内的组数。

示例:

DATA:
COL1:           COL2:
CC2SYS_LOAD     2239
CC2SYS_LOAD     2307
CC2SYS_LOAD     2316
CC2SYS_LOAD     7164
CC2SYS_LOAD     7169
CC2SYS_LOAD     7179

我需要它 return 计数为 2,因为有 2 组 3 条记录,其中 COL2 的值彼此下降 900。

可能有 100 或 none。

试试这个:

select
    sum(flag) + 1
from (
    select
        case when col2 - lag(col2,1,col2) over (order by col2) > 900 then 1 end flag
    from your_table
) t

flag(使用window函数构建lag)标记下一组的开始。