如何在 oracle select 查询中获取更改点?

How to get change points in oracle select query?

我如何select 更改此数据集的点

1   0
2   0
3   0
4   100
5   100
6   100
7   100
8   0
9   0
10  0
11  100
12  100
13  0
14  0
15  0

我想要这个结果

4  7  100 
11 12 100

此查询基于分析函数 lag()lead() 给出预期输出:

select id, nid, point 
  from (
    select id, point, p1, lead(id) over (order by id) nid 
      from (
        select id, point,
            decode(lag(point) over (order by id), point, 0, 1) p1,
            decode(lead(point) over (order by id), point, 0, 2) p2
         from test)
     where p1<>0 or p2<>0)
 where p1=1 and point<>0

SQLFiddle


编辑:如果只有一行用于 更改点:

,您可能需要更改第 3 行
    ...
    select id, point, p1,
        case when p1=1 and p2=2 then id else lead(id) over (order by id) end nid 
    ...

使用ROW_NUMBER解析函数会很简单,MINMAX.

这是一个关于查找 interval/series 值并跳过间隙的常见问题。我喜欢 Aketi Jyuuzou 给它的词 Tabibitosan method

例如,

SQL> SELECT MIN(A),
  2    MAX(A),
  3    b
  4  FROM
  5    ( SELECT a,b, a-Row_Number() over(order by a) AS rn FROM t WHERE b <> 0
  6    )
  7  GROUP BY rn,
  8    b
  9  ORDER BY MIN(a);

    MIN(A)     MAX(A)          B
---------- ---------- ----------
         4          7        100
        11         12        100

SQL>