![](/img/trans.png)
[英]Oracle query to find rows in a table that have the same values for columns A,B, and C but different values for column D
[英]comparing timestamps in two consecutive rows which have different values for column A and the same value for column B in Big Query
伙計們,我有一個大的查詢結果,這說明我的時間(在列local_time
)是騎手(列rider_id
)的應用程序(列注銷event
),所以有列兩個不同的值event
,“authentication_complete ”和“注銷”。
event_date rider_id event local_time
20200329 100695 authentication_complete 20:07:09
20200329 100884 authentication_complete 12:00:51
20200329 100967 logout 10:53:17
20200329 100967 authentication_complete 10:55:24
20200329 100967 logout 11:03:28
20200329 100967 authentication_complete 11:03:47
20200329 101252 authentication_complete 7:55:21
20200329 101940 authentication_complete 8:58:44
20200329 101940 authentication_complete 17:19:57
20200329 102015 authentication_complete 14:20:27
20200329 102015 logout 22:47:50
20200329 102015 authentication_complete 22:48:34
我想要實現的是對於曾經注銷的每個騎手,在一個列中我想獲得他們注銷的時間,在另一列中我想獲得該注銷事件之后發生的“authentication_complete”事件的時間對於那個騎手。 通過這種方式,我可以看到每個騎手離開應用程序的時間段。 我想得到的查詢結果如下所示。
event_date rider_id time_of_logout authentication_complete_right_after_the_logout
20200329 100967 10:53:17 10:55:24
20200329 100967 11:03:28 11:03:47
20200329 102015 22:47:50 22:48:34
這是一個非常不干凈的數據集,到目前為止我能夠清理這么多,但是在這一步,我感覺很卡。 我正在研究諸如lag()
類的函數,但由於數據是 180,000 行,因此rider_id 可能有多個名為“logout”的事件,並且同一個rider_id 有多個名為“authentication_complete”的連續事件,這更加令人困惑。 我真的很感激任何幫助。 謝謝!
我想你想要lead()
:
select event_date, rider_id, date, local_time as logout_date,
authentication_date
from (select t.*,
lead(local_time) over (partition by event_date, rider_id order by local_time) as authentication_date
from t
) t
where event = 'logout';
這假設下一個事件確實是身份驗證,如您的示例數據中所示。 如果不是這種情況,您不會指定要做什么。
如果您特別想要下一個身份驗證日期,那么您可以使用min()
:
select event_date, rider_id, date, local_time as logout_date,
authentication_date
from (select t.*,
min(case when event = 'authentication_complete' then local_time end) over (partition by event_date, rider_id order by local_time desc) as authentication_date
from t
) t
where event = 'logout';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.