0

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

我正在寻找所有在 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
4

2 回答 2

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)

The correlated subquery is getting the max time for each row that the main query is retrieving. In this case we are getting the max time for each event, event_date and user.

于 2013-05-17T20:32:06.230 回答
1

您没有说明您的 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 日期文字使日期解析更加稳定,并且独立于任何语言/环境设置。

于 2013-05-17T22:02:45.303 回答