计算一行中的连续(特定)匹配,但如果其他内容在中间,则从头开始。
我的想法是查看某人在一段时间内(在我的情况下为15分钟)内进行了多少次登录尝试并采取相应措施。例如(简化表):
select count(Loginstatus) Result from Userlog
where User_Id = 5
and Loginstatus = 'ATTEMPT'
and Timestamp > Systimestamp - Interval '15' Minute;
我的选择现在应该产生:2
到目前为止我得到了什么:
File "C:\Users\X\AppData\Local\Programs\Python\Python35\lib\site-packages\pyassimp\helper.py", line 234, in search_library
raise AssimpError("assimp library not found")
pyassimp.errors.AssimpError: assimp library not found
但不幸的是,这计算了所有的尝试。我很高兴得到任何帮助,并提前感谢你
答案 0 :(得分:0)
您需要使用GROUP BY:
SELECT [LoginStatus]=MAX(Loginstatus), [Count]=count(Loginstatus)
FROM Userlog
WHERE User_Id = 5
AND Loginstatus = 'ATTEMPT'
AND Timestamp > Systimestamp - Interval '15' Minute;
GROUP BY (Loginstatus)
答案 1 :(得分:0)
您可以使用行号的差异来识别相邻序列:
select user_id, count(*) as num_consecutive
from (select ul.*,
row_number() over (partition by user_id order by timestamp) as seqnum_u,
row_number() over (partition by user_id, loginstatus order by timestamp) as seqnum_us
from Userlog ul
where User_Id = 5 and Timestamp > Systimestamp - Interval '15' Minute;
) ul
where Loginstatus = 'ATTEMPT'
group by user_id, (seqnum_u - seqnum_us);
如果您想要一个值,可以添加:
order by num_consecutive desc
fetch first 1 row only