SQL在某个Date之后获取每个ID的LAST记录

时间:2013-05-15 06:04:01

标签: sql oracle distinct

我需要帮助创建一个SQL语句,以便在USER_LOG表中的每个USERID的某个日期之后检索LAST记录。

我忘了写这个表中的其他几列。

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: Get All users they 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 THIS
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 THIS
Example 2: Get All users they 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 THIS

我找到了一些东西,但我不知道在X之后我们把条件与日期放在一起:

SELECT u1.* FROM user_log u1 LEFT JOIN user_log u2 ON (u1.id = u2.id AND u1.modify_date < u2.modify_date ) WHERE u2.modify_date IS NULL;

有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:5)

嗯,出于这些目的,他们自己发明了analytical functions

你在这里:

SELECT DISTINCT
  ID,
  NAME, 
  LAST_VALUE(MODIFY_DATE) OVER (PARTITION BY ID ORDER BY MODIFY_DATE ROWS BETWEEN unbounded preceding and unbounded following) MODIFY_DATE
FROM
  USER_LOG
WHERE
  MODIFY_DATE > :date

答案 1 :(得分:1)

如果您想要在特定日期之后的最短日期(按用户数据分组),您应该执行以下操作:

SELECT ID, NAME, MIN(MODIFY_DATE) FROM user_log
WHERE MODIFY_DATE > ?
GROUP BY ID, NAME

答案 2 :(得分:1)

以下查询将为您提供某个日期X后每位用户的最后一条记录。

select * 
  from user_log
 where (id, modify_date) in(
         select id, max(modify_date)
           from user_log
          where modify_date > date '2013-05-12'
          group by id);

子查询选择2013-05-12之后的所有记录。在这些记录中,它为每个用户选择最后(最大)modify_Date。外部查询只返回整行。

答案 3 :(得分:0)

SELECT ID, NAME, MAX(MODIFY_DATE) FROM user_log
WHERE MODIFY_DATE > datevalue
GROUP BY ID, NAME