有一张表table有两个字段A,B 
要求以A字段分组然后对每一个A的值的所有记录按B排序,然后根据B的值选出类似rownum的序列
比如
A   B
11  62
12  38
11  24
12  72
12  39
……
最后的结果为
A  B  rownum
11 24  1
11 62  2
12 38  1
12 39  2
12 72  3
……
请问这个SQL语句怎么写啊?

解决方案 »

  1.   

    select a,b,rownum()over(partition by a,order by b) rownumm from table 
      

  2.   

    select * from
        (select * ,row_number()over(order by b ) orderNumber from table order a,b )大概是这样的
      

  3.   

    你也可以使用rank()over(),dense_rank()over()
      

  4.   


    select a,b,row_number() over(partition by a,order by b) rownumm 
    from tablename 
    order by a
      

  5.   


    with tb as(
    select 11 A,62 B from dual union all
    select 12, 38 from dual union all
    select 11, 24 from dual union all
    select 12, 72 from dual union all
    select 12, 39 from dual)
    select a,b,row_number() over(partition by a order by b) rn
    from tb         A          B         RN
    ---------- ---------- ----------
            11         24          1
            11         62          2
            12         38          1
            12         39          2
            12         72          3
      

  6.   

    select a,b,row_number() over(partition by a order by b) rn from tb
      

  7.   

    select a,b,row_number() over(partition by a order by b) rn
    from tb