【问题标题】:How to get the most recent record for a user within a day over a period of time如何获取用户在一天内一段时间内的最新记录
【发布时间】:2013-05-17 20:05:39
【问题描述】:

我有一个数据集,其中记录了用户在一段时间内的不同活动,因此我有多个用户在不同的日子有多个活动。

我正在寻找所有在 15 日和 16 日提交了事件 B 的用户,然后只想提取最后一次发生的事件 B,以防多个事件存在。

示例数据集:

User Event Event_Date Event_Time
==== ===== ========== =================================
  1   A    15-May-13  15-MAY-13 05.46.20.000000000 AM
  2   A    15-May-13  15-MAY-13 09.33.22.000000000 AM
  3   A    15-May-13  15-MAY-13 09.47.50.000000000 AM
  4   A    15-May-13  15-MAY-13 09.59.53.000000000 AM
  5   A    15-May-13  15-MAY-13 10.12.25.000000000 AM
  1   B    15-May-13  15-MAY-13 05.46.20.000000000 AM
  1   B    15-May-13  15-MAY-13 09.33.22.000000000 AM
**1   B    15-May-13  15-MAY-13 09.47.50.000000000 AM**
**3   B    15-May-13  15-MAY-13 09.59.53.000000000 AM**
  5   B    15-May-13  15-MAY-13 10.12.25.000000000 AM
**5   B    15-May-13  15-MAY-13 10.30.25.000000000 AM**
  1   A    16-May-13  16-MAY-13 01.23.00.000000000 AM
  1   B    16-May-13  16-MAY-13 01.28.35.000000000 AM
**1   B    16-May-13  16-MAY-13 01.28.43.000000000 AM**
  3   A    16-May-13  16-MAY-13 08.38.06.000000000 PM
**3   B    16-May-13  16-MAY-13 12.05.53.000000000 AM**
  4   A    16-May-13  16-MAY-13 12.21.57.000000000 AM
**4   B    16-May-13  16-MAY-13 05.21.57.000000000 PM**

结果集应如下所示,其中包含有事件 B 的所有用户、特定日期的最后一个事件的事件日期(如果事件有多个记录)和日期。

User Event Event_Date Event_Time
==== ===== ========== =================================
 1    B    15-May-13   
 3    B    15-May-13   
 5    B    15-May-13   
 1    B    16-May-13   
 3    B    16-May-13   
 4    B    16-May-13

下面的查询给了我一天的正确结果,但是当我尝试一系列日期时,它只给出最近的事件。

select user, event, event_date, max(event_time)
from table_A where event = 'B'
and event_date = '15-May-13'
group by user, event, event_date

【问题讨论】:

  • 您是否尝试从 where 子句中删除 and event_date ='15-May-13'?然后您将返回所有日期。当您使用日期范围时,您使用的查询是什么?
  • 你用的是什么数据库?
  • 为什么要存储event_date event_timeevent_time 是一个时间戳,包含与event_date 列相同的日期。对我来说似乎很多余。

标签: sql


【解决方案1】:
select a1.user, a1.event, a1.event_date, a1.event_time
from table_A a1 
where a1.event ='B' 
and a1.event_date <='15-May-13'
and a1.event_date >='01-May-13'
and a1.event_time = (select max(event_time) 
                 from table_A a2
                 where a2.event = a1.event 
                 and a2.event_date = a1.event_date
                 and a2.user = a1.user)

相关子查询正在获取主查询正在检索的每一行的最大时间。在这种情况下,我们将获取每个事件、event_date 和用户的最长时间。

【讨论】:

    【解决方案2】:

    您没有说明您的 DBMS,所以这是 ANSI SQL:

    select username, 
           event, 
           event_date, 
           event_time
    from (
        select "USER" as username, 
               event, 
               event_date, 
               event_time
               row_number() over (partition by "USER", event order by event_time desc) as rn
        from table_a
        where event = 'B' 
          and event_date between date '2013-05-13' and date '2013-05-15'
    ) t
    where rn = 1;
    

    请注意,USER 是保留字,因此需要引用(为方便起见,我将其“重命名”)。我还使用 ANSI 日期文字使日期解析更加稳定,并且独立于任何语言/环境设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多