需求如下:
table_a 表中有2个字段.   col_num,col_art.字段值如下col_num       col_art
-----------------------
20091121        0
20091121        0
20091121        0
20091122        0
20091122        1
20091122        0
20091123        1
20091123        0
20091123        2从上面的结果中很容易看出 20091121 的为一组.
判断col_art有重复的就让它加1.最终结果col_num       col_art
-----------------------
20091121        0
20091121        1
20091121        2
20091122        0
20091122        1
20091122        2
20091123        1
20091123        0
20091123        2
期望高手出现...sql或过程实现都可以.

解决方案 »

  1.   

    select col_num , row_number() over (partition by col_num order by col_num)-1 from table_a 
      

  2.   

    我的思路:
    用trigger实现比较简单,在after insert 事件创建trigger,该trigger
    根据col_num的插入值进行 select Max(col_art)操作,获取最大值并加1
    来更新col_art字段值
      

  3.   


    with tt as (
    select '20091121' col_num from dual
    union all
    select '20091121' from dual
    union all
    select '20091121' from dual
    union all
    select '20091122' from dual
    union all
    select '20091122' from dual
    union all
    select '20091122' from dual
    union all
    select '20091123' from dual
    union all
    select '20091123' from dual
    union all
    select '20091123' from dual
    union all
    select '20091123' from dual
    )SELECT col_num , row_number() over (partition by col_num order by col_num)-1 "col_art" from tt;
    COL_NUM     col_art
    -------- ----------
    20091121          0
    20091121          1
    20091121          2
    20091122          0
    20091122          1
    20091122          2
    20091123          0
    20091123          1
    20091123          2
    20091123          3
      

  4.   


    rank, dense_rank,row_number 分析函数
    Rank,Dense_rank,Row_number函数为每条记录产生一个从1开始至N的自然数,N的值可能小于等于记录的总数。这3个函数的唯一区别在于当碰到相同数据时的排名策略。
    ① ROW_NUMBER:Row_number函数返回一个唯一的值,当碰到相同数据时,排名按照记录集中记录的顺序依次递增。 
    ② DENSE_RANK:Dense_rank函数返回一个唯一的值,除非当碰到相同数据时,此时所有相同数据的排名都是一样的。 
    ③ RANK:Rank函数返回一个唯一的值,除非遇到相同的数据时,此时所有相同数据的排名是一样的,同时会在最后一条相同记录和下一条不同记录的排名之间空出排名。
    示例:
    /* Formatted on 2009/11/08 20:48 (Formatter Plus v4.8.8) */SELECT ename, deptno, sal,       RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) "RANK",       DENSE_RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) "DENSE_RANK",       ROW_NUMBER () OVER (PARTITION BY deptno ORDER BY sal DESC) "ROW_NUMBER"  FROM scott.emp本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tianlesoftware/archive/2009/11/10/4795632.aspx
      

  5.   

    create or replace procedure test_proc
    as
    cursor c is SELECT A.col_num,
    row_number() OVER(PARTITION BY col_num ORDER BY col_num asc) ROW_NUMBER from table_a A;
    begin
    for v in c loop
    if(v.ROW_NUMBER >1)then
    delete from table_a where col_num = v.col_num;
    for i in 0.. (v.ROW_NUMBER-1) loop
    insert into table_a(col_num,col_art)values(v.c2,i);
    end loop;
    end if;
    end loop;
    commit;
    end;
    /
      

  6.   

    create or replace procedure test_proc
    as
    cursor c is SELECT A.col_num,
    row_number() OVER(PARTITION BY col_num ORDER BY col_num asc) ROW_NUMBER from table_a A;
    begin
    for v in c loop
    if(v.ROW_NUMBER >1)then
    delete from table_a where col_num = v.col_num;
    for i in 0.. (v.ROW_NUMBER-1) loop
    insert into table_a(col_num,col_art)values(v.col_num,i);
    end loop;
    end if;
    end loop;
    commit;
    end;
    /
      

  7.   


    谢谢 sxq129601 的回答,一句sql搞定 3000多万数据 谢谢!也谢谢其他朋友的回答.