2015-04-15 15 views
0

我正在使用Oracle数据库集成器(ODI)为新表创建接口。为Oracle中的每个重复值自动递增

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

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

接口将在目标表输出如下:

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

我试图通过进入不同的SQL查询到在实现文本编辑器来做到这一点映射属性,但似乎我实际上无法使其逐渐增加。

任何帮助将不胜感激!

回答

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> 
+0

你可以简单地做'ORDER BY NULL'或'ORDER BY rownum'在这种情况下 - 我会用'ROW_NUMBER()''以上RANK()'的情况下,有另一列的OP要用于订购。 –

+0

@DavidFaber,只有一次按rownum命令,不能保证所需的排序,OP需要在'over()'部分执行'两次'一个命令,另一个在查询结束时执行,但是感谢你的评论,回答编辑。 – jfun

+0

对,我建议'ORDER BY rownum'只在所需的'ORDER BY'子句中有* something *。我不知道是否有OP的一部分是否需要。 –