oracle 用于为重复项创建序列的sql函数- [duplicate]

mwkjh3gx  于 2023-02-18  发布在  Oracle
关注(0)|答案(1)|浏览(125)
    • 此问题在此处已有答案**:

Add a sequence column in a query(1个答案)
4天前关闭。
我想知道是否有一个简单的sql函数可以对每个重复记录进行顺序分类,如图所示:
enter image description here

o4tp2gmn

o4tp2gmn1#

看起来像解析函数之一,例如row_number(或者rank)。
样本数据:

SQL> with test (product, value) as
  2    (select 'A', 100 from dual union all
  3     select 'B', 100 from dual union all
  4     select 'C', 100 from dual union all
  5     select 'A', 100 from dual union all
  6     select 'B', 100 from dual union all
  7     select 'A', 100 from dual union all
  8     select 'D', 100 from dual union all
  9     select 'D', 100 from dual union all
 10     select 'F', 100 from dual union all
 11     select 'F', 100 from dual union all
 12     select 'F', 100 from dual
 13    )

质询:

14  select product, value,
 15    row_number() over (partition by product order by null) classification
 16  from test
 17  order by product;

PRODUCT         VALUE CLASSIFICATION
---------- ---------- --------------
A                 100              1
A                 100              2
A                 100              3
B                 100              1
B                 100              2
C                 100              1
D                 100              1
D                 100              2
F                 100              1
F                 100              2
F                 100              3

11 rows selected.

SQL>

相关问题