1. 程式人生 > >oracle分析函式row_number的例項應用

oracle分析函式row_number的例項應用

row_number() over (partition by col1 order by col2)
表示根據col1分組,在分組內部根據 col2排序
而這個值就表示每組內部排序後的順序編號(組內連續的唯一的)

表內容如下:
name | seqno | description
A | 1 | test
A | 2 | test
A | 3 | test
A | 4 | test
B | 1 | test
B | 2 | test
B | 3 | test
B | 4 | test
C | 1 | test
C | 2 | test
C | 3 | test
C | 4 | test

我想有一個sql語句,搜尋的結果是
A | 1 | test
A | 2 | test
B | 1 | test
B | 2 | test
C | 1 | test
C | 2 | test
實現:
select name,seqno,description
from(select name,seqno,description,row_number()over(partition by name order by seqno)id
from table_name) where id<=3;