oracle - SQL get LAST record for each ID after some Date -
i need creating sql statement retrieving last record after date each userid in user_log table.
i forgot write there few other columns in table.
table: user_log
id name modify_date modify_type 55 usera 2013-05-07 15:47:53.0 1 88 userb 2013-05-07 16:00:57.0 1 55 usera 2013-05-08 11:44:10.0 2 88 userb 2013-05-08 15:47:09.0 2 88 userb 2013-05-08 16:01:41.0 1 55 usera 2013-05-09 15:11:53.0 0 55 usera 2013-05-09 16:00:57.0 0 55 usera 2013-05-10 09:14:10.0 1 88 userb 2013-05-10 16:01:41.0 2 55 usera 2013-05-10 18:23:03.0 2 55 usera 2013-05-11 09:14:10.0 2 88 userb 2013-05-11 16:01:41.0 1 55 usera 2013-05-13 11:34:07.0 1 55 usera 2013-05-13 15:53:04.0 2 55 usera 2013-05-13 16:13:04.0 1
example 1: users have bean changed after '2013-05-08 00:00:00.0'. must return:
id name modify_date modify_type 55 usera 2013-05-07 15:47:53.0 1 88 userb 2013-05-07 16:00:57.0 1 55 usera 2013-05-08 11:44:10.0 2 88 userb 2013-05-08 15:47:09.0 2 88 userb 2013-05-08 16:01:41.0 1 55 usera 2013-05-09 15:11:53.0 0 55 usera 2013-05-09 16:00:57.0 0 55 usera 2013-05-10 09:14:10.0 1 88 userb 2013-05-10 16:01:41.0 2 55 usera 2013-05-10 18:23:03.0 2 55 usera 2013-05-11 09:14:10.0 2 **88 userb 2013-05-11 16:01:41.0 1** return 55 usera 2013-05-13 11:34:07.0 1 55 usera 2013-05-13 15:53:04.0 2 **55 usera 2013-05-13 16:13:04.0 1** return
example 2: users have bean changed after '2013-05-12 00:00:00.0'. must return:
id name modify_date modify_type 55 usera 2013-05-07 15:47:53.0 1 88 userb 2013-05-07 16:00:57.0 1 55 usera 2013-05-08 11:44:10.0 2 88 userb 2013-05-08 15:47:09.0 2 88 userb 2013-05-08 16:01:41.0 1 55 usera 2013-05-09 15:11:53.0 0 55 usera 2013-05-09 16:00:57.0 0 55 usera 2013-05-10 09:14:10.0 1 88 userb 2013-05-10 16:01:41.0 2 55 usera 2013-05-10 18:23:03.0 2 55 usera 2013-05-11 09:14:10.0 2 88 userb 2013-05-11 16:01:41.0 1 55 usera 2013-05-13 11:34:07.0 1 55 usera 2013-05-13 15:53:04.0 2 **55 usera 2013-05-13 16:13:04.0 1** return
i found don't know put condition date after x:
select u1.* user_log u1 left join user_log u2 on (u1.id = u2.id , u1.modify_date < u2.modify_date ) u2.modify_date null;
can please me this?
well, these purposes analytical functions invented.
here are:
select distinct id, name, last_value(modify_date) on (partition id order modify_date rows between unbounded preceding , unbounded following) modify_date user_log modify_date > :date
Comments
Post a Comment