我想把左边的col3中生成右边的这种序号,有没有简单一点的办法啊?col1 col2 col3 col1 col2 col3
1 a 1 a 1
2 b 2 b 1
3 b 3 b 2
4 b 4 b 3
5 c 5 c 1
6 d 6 d 1
7 e 7 e 1
8 e 8 e 2
9 e 9 e 3
10 e 10 e 4

解决方案 »

  1.   

    就是这样的,左边是转换前的,右边是期待的结果,本来col3没有值,我需要根据数据的规则来生成这样的序号,不知道有没有人能帮帮我,我在线等啊万分感谢
    col1 col2 col3       col1 col2 col3 
    1    a                1    a    1 
    2    b                2    b    1 
    3    b                3    b    2 
    4    b                4    b    3 
    5    c                5    c    1 
    6    d                6    d    1 
    7    e                7    e    1 
    8    e                8    e    2 
    9    e                9    e    3 
    10   e                10   e    4 
      

  2.   

    -- 查ORACLE分析函数:ROW_NUMBER ... 根据e字段进行PARTITION
      

  3.   

    select rank() over(partition by col2 order by col1) col3 ,col2,col1 from table
      

  4.   

    select rank() over(partition by col2 order by col1) col3 ,col2,col1 from table
      

  5.   

    select col1,col2, (select count(col2) from tableName where col1<=a.col1 and col2=a.col2 ) as col3 from tableName a
      

  6.   

    正好用分析函数,以上几位的稍加修改就能得到你想要的
    update table a
    set a.col3=(
    select row_number() over(partition by b.col2 order by b.col1) col3 from table b
    where a.col1=b.col1
    )
      

  7.   

    update tb t1 set t1.col3 = t2.col3
    (select col1, col2, row_number() over(partition by col2 order by col1) col3
    from tb) t2
      

  8.   


    update tb t1 set t1.col3 = t2.col3 
    (select col1, col2, row_number() over(partition by col2 order by col1) col3 
    from tb) t2