mysql按时间分组不对_MySQL按时间分组结果

bd96500e110b49cbb3cd949968f18be7.png

I have a table that contains timestamps of session events.

I want to query how many sessions I have according to the timestamps when 2 sessions are separated by at least 10 minutes difference of events.

Can I count the sessions with an MySql query ?

Thanks

解决方案

With little info on your tables this is no more than a basic idea for you, but you could do something like this:-

SELECT COUNT(*)

FROM

(

SELECT a.TimeStamp AS ThisTimeStamp, MIN(b.TimeStamp) AS NextTimeStamp

FROM SomeTable a

INNER JOIN SomeTable b

ON a.TimeStamp < b.TimeStamp

GROUP BY a.TimeStamp

) Sub1

WHERE Sub1.ThisTimeStamp < (Sub1.NextTimeStamp - 600)

Get all the timestamp and join them against all other timestamps that are greater, and use MIN to narrow that down to the next greatest timestamp. Then from that select the count where the difference is less than 600 seconds (assuming unix timestamps).

EDIT - If you want the tied down for the number of 10 minute+ gaps in events for users then:-

SELECT COUNT(*)

FROM

(

SELECT a.user_id, a.TimeStamp AS ThisTimeStamp, MIN(b.TimeStamp) AS NextTimeStamp

FROM SomeTable a

INNER JOIN SomeTable b

ON a.TimeStamp < b.TimeStamp

AND a.user_id = b.user_id

GROUP BY a.user_id, a.TimeStamp

) Sub1

WHERE Sub1.ThisTimeStamp < (Sub1.NextTimeStamp - 600)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值