sql查找给定会话中的时差

nx7onnlm  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(339)

请原谅,我不太熟悉sql来完成下面的工作。但如果可能的话,我很想学习如何去做。
我只有一个表:表名sessiontracker

-----------------------------------------------------------------------------------------------------------------------------------------

bundleID                   | sessionId |                  deviceID |                                   eventType |          Timestamp    
-----------------------------------------------------------------------------------------------------------------------------------------

com.package.random          3871207406642403679         333333-00000-0000-00000-000000000000000       REQUEST             1595858140614
com.package.random          3871207406642403679         333333-00000-0000-00000-000000000000000       EVENT               1595857661417
com.package.random          3871207406642403679         333333-00000-0000-00000-000000000000000       RESPONSE            1595857662129
com.package.random          3245233406642403679         000000-00000-0000-00000-000000000000000       REQUEST             1595857661418
com.package.random          3245233406642403679         000000-00000-0000-00000-000000000000000       EVENT               1595857661418
com.package.random          3245233406642403679         000000-00000-0000-00000-000000000000000       RESPONSE            1595857661418
com.package.random          871207406643e243433         000000-00000-0000-00000-000000000000000       REQUEST             1595857662129
com.package.random2         3243254325454535422         111111-00000-0000-00000-000000000000000       REQUEST             1595857662129 
com.package.random3         4353453452525252465         222222-00000-0000-00000-000000000000000       REQUEST             1595857662129  
com.package.random4         3453656456353252345         111111-00000-0000-00000-000000000000000       REQUEST             1595857662129  
com.package.random5         4567568765745634563         111111-00000-0000-00000-000000000000000       REQUEST             1595857662129

我想显示一个列,显示会话中请求、事件和响应之间的时间差。
从上面的例子来看:
我希望能够检查给定会话中eventype时间之间的时间差。因此,显示sessionid=3871207406642403679和eventtype的时间戳差异的额外列是request,event,response,显示它们之间的差异。
我希望databricks中的查询能起作用,这可能吗?
编辑:
我知道我可以用它来获得更易于阅读的时间戳:从unixtime(dataframe.timestamp/1000,“hh:mm:ss”)
但是我不确定如何比较每个请求、事件和响应的时间戳

ylamdve6

ylamdve61#

您可以使用条件聚合来计算每个事件的时间:

select sessionid,
       min(case when event = 'REQUEST' then timestamp end) as request_ts,
       min(case when event = 'EVENT' then timestamp end) as event_ts,
       min(case when event = 'RESPONSE' then timestamp end) as response_ts
from t
group by sessionid;

然后可以对这些表达式使用算术运算:

select s.*,
       (event_ts - request_ts) as time_to_request,
       (response_ts - event_ts) as time_to_event
from (select sessionid,
             min(case when event = 'REQUEST' then timestamp end) as request_ts,
             min(case when event = 'EVENT' then timestamp end) as event_ts,
             min(case when event = 'RESPONSE' then timestamp end) as response_ts
      from t
      group by sessionid
     ) s

相关问题