oracle 更新每个id的日期最大的记录

表a
id totime oid
1 2013-01-03 0001
1 2013-03-05 0002
1 2013-03-09 0003
2 9999-12-31 0004
3 2012-08-07 0005
现在需要进行如下更新
将表中每个id的日期最大的记录的totime 更新为当月最后一天,更新后的结果应该如下
id totime oid
1 2013-01-03 0001
1 2013-03-05 0002
1 2013-03-31 0003 --这条变
2 9999-12-31 0004
3 2012-08-31 0005 --这条变

测试数据

 create table a
(id int,
totime date,
oid varchar2(4));

insert into a values (1,to_date('2013-1-1','yyyy-mm-dd'),'0001');
insert into a values (1,to_date('2013-3-5','yyyy-mm-dd'),'0002');
insert into a values (1,to_date('2013-3-9','yyyy-mm-dd'),'0003');
insert into a values (2,to_date('9999-12-31','yyyy-mm-dd'),'0004');
insert into a values (3,to_date('2012-8-7','yyyy-mm-dd'),'0005');

执行

update a set totime=last_day(totime) where oid in
(select oid from a where exists
(select 1 from
(select id,max(totime) totime from a group by id) t
where a.id=t.id and a.totime=t.totime))追问

第三行的select 1 from 是什么意思
需要改成什么
--我把表名 字段名字都按生产系统上的改了,这个1需要改吗 OID是16进制的

追答

你只改表名就行,那个1你换不换,或者换成什么没关系
就是exists ,那后边是一大整句,判断日期如果是每个id的最大日期才会更新,其实你这个最后更新的是3条,分别是第3,4,5条,只是因为第4条最大日期和他本身一致而已

执行前备份一下原表吧

温馨提示:答案为网友推荐,仅供参考