oracle sql语句分类查询问题(详见补充说明)

来源:百度知道 编辑:UC知道 时间:2024/05/22 20:38:41
比如有这么个表
字段id 时间 字段1
a 2005 m
a 2006 n
b 2005 o
b 2004 p
b 2003 q
想用一条语句查出每个id最近一个时间的字段1的值
即结果是
a 2006 n
b 2005 o

就是根据id分组,oracle可以使用分析函数比较简单

select id,时间,字段1
from
(
select t.*,row_number() over(partition by id order by 时间 desc) rn
from tablename t
)
where rn =1;