所以我有这些原始数据:
TYPE_DESCRIPTION TYPE_ORDER
PROFILING 1
RIBBONNING 2
RIBBONNING 3
EXTRUSION 4
SPIRALING 5
SPIRALING 6
RIBBONNING 7
ARMOURING 8
RIBBONNING 9
RIBBONNING 10
ARMOURING 11
RIBBONNING 12
RIBBONNING 13
RIBBONNING 14
RIBBONNING 15
RIBBONNING 16
RIBBONNING 17
RIBBONNING 18
RIBBONNING 19
EXTRUSION 20
我要从TYPE_DESCRIPTION分组中获取行号,但是考虑到TYPE_ORDER字段,当分组更改时必须重置计数。 基本上我期望的结果是:
TYPE_DESCRIPTION TYPE_ORDER ROW_NO_BY_GROUP
PROFILING 1 1
RIBBONNING 2 1
RIBBONNING 3 2
EXTRUSION 4 1
SPIRALING 5 1
SPIRALING 6 2
RIBBONNING 7 1
ARMOURING 8 1
RIBBONNING 9 1
RIBBONNING 10 2
ARMOURING 11 1
RIBBONNING 12 1
RIBBONNING 13 2
RIBBONNING 14 3
RIBBONNING 15 4
RIBBONNING 16 5
RIBBONNING 17 6
RIBBONNING 18 7
RIBBONNING 19 8
EXTRUSION 20 1
请注意,RIBBONNING值会在多行中重复,但是每次TYPE_DESCRIPTION更改时,ROW_NUMBER都会重置。
谢谢..
答案 0 :(得分:2)
您可以使用get
形成连续的组:
select type_description, type_order,
row_number() over (order by type_order)
- row_number() over (partition by type_description order by type_order) as group_no
from your_table;
TYPE_DESCRIPTION TYPE_ORDER GROUP_NO
---------------- ---------- ----------
PROFILING 1 0
RIBBONNING 2 1
RIBBONNING 3 1
EXTRUSION 4 3
SPIRALING 5 4
SPIRALING 6 4
RIBBONNING 7 4
ARMOURING 8 7
RIBBONNING 9 5
RIBBONNING 10 5
ARMOURING 11 9
RIBBONNING 12 6
RIBBONNING 13 6
RIBBONNING 14 6
RIBBONNING 15 6
RIBBONNING 16 6
RIBBONNING 17 6
RIBBONNING 18 6
RIBBONNING 19 6
EXTRUSION 20 18
然后获取这些组中的行号:
select type_description, type_order,
row_number() over (partition by type_description, group_no
order by type_order) as row_no_by_group
from (
select type_description, type_order,
row_number() over (order by type_order)
- row_number() over (partition by type_description order by type_order) group_no
from your_table
)
order by type_order;
TYPE_DESCRIPTION TYPE_ORDER ROW_NO_BY_GROUP
---------------- ---------- ---------------
PROFILING 1 1
RIBBONNING 2 1
RIBBONNING 3 2
EXTRUSION 4 1
SPIRALING 5 1
SPIRALING 6 2
RIBBONNING 7 1
ARMOURING 8 1
RIBBONNING 9 1
RIBBONNING 10 2
ARMOURING 11 1
RIBBONNING 12 1
RIBBONNING 13 2
RIBBONNING 14 3
RIBBONNING 15 4
RIBBONNING 16 5
RIBBONNING 17 6
RIBBONNING 18 7
RIBBONNING 19 8
EXTRUSION 20 1