如:一张tmaterialmodify表 字段 objid,materialid(备件id),code(备件编号),name(备件名称),modifytime(datetime类型) 修改时间 objid 唯一标识列 数据: '1233','11111','A','螺帽','2007-01-01 18:00:00' '1234','22222','B','车盖','2008-02-04 08:00:00' '1235','11111','A','螺帽','2008-04-01 17:00:00' '1236','11111','A','螺帽','2006-07-11 11:30:00' '1237','22222','B','车盖','2005-03-02 16:00:00' '1238','22222','B','车盖','2007-01-01 18:00:00' '1239','22222','B','车盖','2006-11-01 13:00:00' '1240','11111','A','螺帽','2004-01-01 18:00:00' ........ 想要得结果是显示在一个时间段内每个备件最近修改的三条记录 查询从修改时间 2006-6-30 00:00:00 ----2008-04-30 00:00:00 的备件修改记录 结果 '1236','11111','A','螺帽','2006-07-11 11:30:00' '1233','11111','A','螺帽','2007-01-01 18:00:00' '1235','11111','A','螺帽','2008-04-01 17:00:00' '1239','22222','B','车盖','2006-11-01 13:00:00' '1238','22222','B','车盖','2007-01-01 18:00:00' '1234','22222','B','车盖','2008-02-04 08:00:00'
sql:>select * from (select t.*,row_number() over (partition by materialid order by modifytime desc) rn from tmaterialmodify t where t.modifytime between to_date('2006-6-30','yyyy-mm-dd') and to_date('2008-09-30','yyyy-mm-dd')) where rn <=3 /
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;