select * from (select code col1, col2, row_number() over (partition by code order by time desc) rank from t_order where type= '107' and to_char(sysdate, 'yyyymmdd-hh24:mi:ss') <= validuntilTime and code in ('a','b','c') ) where rank = 1
<!-- more -->sql
to_char(sysdate, 'yyyymmdd-hh24:mi:ss') <= validuntilTime
)且code爲a,或b,或c(code in('a','b','c')
)的全部記錄根據code分組(partition by code
),且組內的順序是根據時間的逆序排列(order by time desc
)(select code col1, col2, row_number() over (partition by code order by time desc) rank from t_order where type= '107' and to_char(sysdate, 'yyyymmdd-hh24:mi:ss') <= validuntilTime and code in ('a','b','c') )
CODE | COL1 | COL2 | RANK |
---|---|---|---|
a | xxx | xxx | 1 |
a | xxx | xxx | 2 |
a | xxx | xxx | 3 |
b | xxx | xxx | 1 |
b | xxx | xxx | 2 |
c | xxx | xxx | 1 |
c | xxx | xxx | 2 |
c | xxx | xxx | 3 |
c | xxx | xxx | 4 |
select * from (上面的代碼) where rank = 1
就能將每一組內的一條最新的記錄查詢出來了