我想根据时间间隔过滤记录,但是有时间戳记。我有一个带有以下列名的表“ df”:
id-int时间-时间戳正确性-布尔主题-文本
每个学生(id)都完成有关特定主题(主题)的任务。如果分配正确完成,则系统在“正确性”列中分配“真”值,否则分配“假”值。系统还将保存学生完成任务的时间(时间)。
我需要编写一个sql查询,以计算2020年3月在一小时内成功完成20个任务的所有学生。我需要计算从实际开始时间和实际完成时间开始的一个小时内完成任务的学生。
提前致谢!
您可以使用窗口函数和范围框:
select distinct id
from (
select
t.*,
count(*) filter(where correctness) over(
partition by id
order by time
range between interval '1 hour' preceding and current row
) cnt
from mytable t
where time >= date '2020-03-01' and time < date '2020-04-01'
) t
where cnt > 20
窗口功能可计算同一用户在过去一个小时内成功执行了多少任务;然后,您可以使用此信息来筛选结果集。
这将为您提供满足条件的用户列表。如果您要计算该用户的数量,请替换select distinct id
为select count(distinct id)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句