根据数字变化创建顺序增加的组

Creating sequentially increasing groups based on number change

如何在 oracle SQL 中对此进行编码? 我有以下数据 Current Result

我想生成如下所示的结果: Desired Result

所以,我基本上希望组 ID 随着行号变回 1 而增加。我正在尝试使用 row_number、rank() 和分区函数,但它无法正常工作。急切求助! 谢谢

编辑(戈登):

原来的问题有问题的数据。将问题中的值作为文本比引用图像要好得多,所以我将其添加回:

Code   Row Number
214     1
214     2
210     1
210     2
210     3
214     1

我想生成如下所示的结果:

Code   Row Number  Group Id
214     1          1
214     2          1
210     1          2
210     2          2
210     3          2
214     1          3

为了执行您想要的操作,您需要一个列来指定 table 中行的顺序。让我假设您有一个 ID 或创建日期或类似的东西。

如果是这样,那么你想要的只是第二列为1的次数的累加和:

select t.*,
       sum(case when RowNumber = 1 then 1 else 0 end) over (order by id) as GroupId
from t;

仍然不清楚哪个字段是 ID,因为如果它是你所说的行号,它不会按照你在预期输出中的方式工作

create table test (id int , code int, rownumber int);
insert into test values (1,214,1);
insert into test values (2,214,2);
insert into test values (3,210,1);
insert into test values (4,210,2);
insert into test values (5,210,3);
insert into test values (6,214,1);


select s.code,  sum(add_group) over (order by id) from (
select id, code, case when rownumber=1 then 1 else 0 end as add_group from test
order by id
) s

    CODE    SUM(ADD_GROUP)OVER(ORDERBYID)
1   214 1
2   214 1
3   210 2
4   210 2
5   210 2
6   214 3

顺便说一下,@Gordon Linoff 的 asnwer 效果更好,而且完全符合您的要求,但您需要为订单添加额外的字段