日期:2014-05-17 浏览次数:20840 次
create table t1 (id number(5),y_id number(5),y_my number(10,2),t_date date); insert into t1 values (1,1,1000,date'2012-01-01'); insert into t1 values (2,2,2000,date'2012-01-01'); insert into t1 values (3,3,3000,date'2012-01-01'); insert into t1 values (4,1,1500,date'2012-02-01'); insert into t1 values (5,2,2500,date'2012-02-01'); insert into t1 values (6,3,3500,date'2012-02-01'); insert into t1 values (7,1,1100,date'2012-03-01'); insert into t1 values (8,3,2200,date'2012-03-01'); insert into t1 values (9,4,1000,date'2012-03-01'); commit; --查询某个员工最后一次记录员 select * from ( select * from t1 where y_id = 1 order by t_date desc ) where rownum < 2 --查询所有员工最后一次记录员 select tt.id,tt.y_id,tt.y_my,tt.t_date from ( select a.*,row_number() over(partition by y_id order by t_date desc) rn from t1 a ) tt where rn = 1