1. 程式人生 > >oracle 按某個欄位分組然後從每組取出最大的一條紀錄

oracle 按某個欄位分組然後從每組取出最大的一條紀錄

欄位有:listid recid
        1       1
        1       2
        2       3
        2       4
        2       5
想得到的資料是     按照   listid  分組   然後取出每組中recid最大的那幾條紀錄。結果應該是這兩條紀錄: 

   欄位:listid  recid

        1       2
        2       5



程式碼:
select  listid,recid   
from ( select listid,recid,row_number() over(partition by listid order by    recid desc) rn  
          from mo_partprg)   t1 where rn=1;