假设我有一个由eventLog
eventTime
eventTime guid
2015/8/13 09:32 gewgew23k
2015/8/13 21:26 fwetyhhhg
2015/8/14 04:23 abcdefghi
2015/8/14 08:33 bfhjrwtwq
2015/8/14 12:02 bfhjrwtwq
2015/8/14 12:02 bfhjrwtwq
2015/8/15 06:00 bfhjrwtwq
2015/8/15 07:24 bfhjrwtwq
2015/8/15 14:26 gweg234fp
2015/8/16 01:00 sfsdfefsd
2015/8/16 01:00 xcvcvdbbz
2015/8/16 01:00 fasfqwfqq
2015/8/16 01:05 bfhjrwtwq
2015/8/31 17:57 zxczxvzbb
2015/8/31 20:29 dssgxcvxc
2015/8/31 23:14 vnvbncdfh
2015/9/1 01:35 ti565iyii
2015/9/1 01:42 qewrwgjpo
2015/9/1 09:33 fwetyhhhg
2015/9/1 13:34 fwetyhhhg
2015/9/1 14:04 fwetyhhhg
2015/9/1 15:03 fwetyhhhg
2015/9/1 16:03 fwetyhhhg
如何找到连续发生4次以上的指针。
这是预期的结果:
2015/8/14 08:33 bfhjrwtwq
2015/8/14 12:02 bfhjrwtwq
2015/8/14 12:02 bfhjrwtwq
2015/8/15 06:00 bfhjrwtwq
2015/8/15 07:24 bfhjrwtwq
2015/9/1 09:33 fwetyhhhg
2015/9/1 13:34 fwetyhhhg
2015/9/1 14:04 fwetyhhhg
2015/9/1 15:03 fwetyhhhg
2015/9/1 16:03 fwetyhhhg
答案 0 :(得分:0)
您可以使用变量枚举值:
select t.*,
(@rn := if(@g = guid, @rn + 1,
if(@g := guid, 1, 1)
)
) as rn
from t cross join
(select @g := '', @rn := 0) params
order by eventTime;
接下来,执行反向排序以将最大值应用于每个组,然后选择:
select t.*
from (select t.*,
(@m := if(@g = guid, @m,
if(@g := guid, rn, rn)
)
) as m
from (select t.*,
(@rn := if(@g = guid, @rn + 1,
if(@g := guid, 1, 1)
)
) as rn
from t cross join
(select @g := '', @rn := 0) params
order by eventTime
) t cross join
(select @g1 := guid, @m := -1) params
from t
order by eventTime desc
) t
where m >= 4;