Oracle中每个重复值的自动增量

时间:2015-04-15 01:13:38

标签: sql database oracle oracle-data-integrator

我正在使用Oracle Database Integrator(ODI)为新表创建一个接口。

我要做的是逐步增加另一列中值的每个重复出现的数字。例如,对于表格:

Person_ID  | ...
32         | ...
45         | ...
32         | ...
67         | ...
45         | ...
45         | ...

接口将在目标表中输出以下内容:

Person_ID | Sequence_Number | ...  
32        |       1         | ...
45        |       1         | ...
32        |       2         | ...
67        |       1         | ...
45        |       2         | ...
45        |       3         | ...

我试图通过在Mapping Properties中的Implementation文本编辑器中输入各种SQL查询来实现这一点,但似乎我实际上无法逐步增加它。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

您可以将row_number()rank()over(partition by personId order by personId)

一起使用
select personId,
       row_number() over(partition by personId order by personId) Sequence_Number
from your_table

修改:如果您希望结果的排序方式与您提到的所需输出完全相同,则需要执行order by rownum两次以保证所需的排序:

select personId,
       row_number() over(partition by personId order by rownum) Sequence_Number
from your_table
order by rownum

以下是对上述查询的检查:


SQL> create table your_table (personId int);

Table created.

SQL> insert all
  2      into your_table values(32)
  3      into your_table values(45)
  4      into your_table values(32)
  5      into your_table values(67)
  6      into your_table values(45)
  7      into your_table values(45)
  8      select * from dual;

6 rows created.

SQL> commit;

Commit complete.

SQL> select personId,
  2         row_number() over(partition by personId order by rownum) Sequence_Number
  3  from your_table;

  PERSONID SEQUENCE_NUMBER
---------- ---------------
        32               1
        32               2
        45               1
        45               2
        45               3
        67               1

6 rows selected.

SQL> select personId,
  2         row_number() over(partition by personId order by rownum) Sequence_Number
  3  from your_table
  4  order by rownum;

  PERSONID SEQUENCE_NUMBER
---------- ---------------
        32               1
        45               1
        32               2
        67               1
        45               2
        45               3

6 rows selected.

SQL> drop table your_table;

Table dropped.

SQL>