我有一个查询,我在其中确定用户提交的特定表单超过1次:
select userid, form_id, count(*)
from table_A
group by userid, form_id
having count(userid) > 1
但是,我试图查看哪些用户在5秒的时间范围内提交了多个表单(我们在此表中有一个提交时间戳的字段)。如何根据该标准缩小此查询范围?
答案 0 :(得分:2)
一种方法是通过DATEDIFF(Second, '2017-01-01', SubmittionTimeStamp) / 5
添加到组中
这将根据userid,form_id和五秒间隔对记录进行分组:
select userid, form_id, count(*)
from table_A
group by userid, form_id, datediff(Second, '2017-01-01', SubmittionTimeStamp) / 5
having count(userid) > 1
请阅读this SO post以获取更详细的说明。
答案 1 :(得分:2)
@nikotromus
您尚未提供有关您的架构和其他可用列的大量详细信息,也未提供有关此信息的使用方式/方式和位置的详细信息。
但是,如果你想这样做,那就是"生活"所以将您的时间结果与当前时间戳进行比较,它看起来像:
SELECT userid, form_id, count(*)
FROM table_A
WHERE DATEDIFF(SECOND,YourColumnWithSubmissionTimestamp, getdate()) <= 5
GROUP BY userid, form_id
HAVING count(userid) > 1
答案 2 :(得分:1)
您可以使用lag
形成彼此相距5秒的行组,然后对它们进行聚合:
select distinct userid,
form_id
from (
select t.*,
sum(val) over (
order by t.submission_timestamp
) as grp
from (
select t.*,
case
when datediff(ms, lag(t.submission_timestamp, 1, t.submission_timestamp) over (
order by t.submission_timestamp
), t.submission_timestamp) > 5000
then 1
else 0
end val
from your_table t
) t
) t
group by userid,
form_id,
grp
having count(*) > 1;
有关更多解释,请参阅此答案:
答案 3 :(得分:0)
我只想使用exists
来吸引用户:
select userid, form_id
from table_A a
where exists (select 1
from table_A a2
where a2.userid = a.userid and a2.timestamp >= a.timestamp and a2.timestamp < dateadd(second, 5, a.timestamp
);
如果您想要点票,只需添加group by
和count(*)
。