SQL Server:每30分钟或更长时间计数一次
我们有一个活动数据库,将用户互动记录到网站,存储一个包括time1
,session_id
和cosition_id
的
2022-05-12 08:00:00|11|1
2022-05-12 08:20:00|11|1
2022-05-12 08:30:01|11|1
2022-05-12 08:14:00|22|2
2022-05-12 08:18:00|22|2
2022-05-12 08:16:00|33|1
2022-05-12 08:50:00|33|1
日志要有两个单独的查询:
查询#1:,如果每天有30分钟或更多的日志,我需要多次计数会话。
例如:最初count = 0
session_id = 11,它从08:00开始,最后一次使用相同的session_id为08:30 -count = 1
session_id = 22它在08:14开始,最后一次使用同一会话为08:14-仍然= 1,因为它少于30分钟
,我尝试了此查询,但是它没有可行的
select
count(session_id)
from
table1
where
@datetime between Time1 and dateadd(minute, 30, Time1);
预期结果:
查询#2:这是上述查询的扩展,我每天都需要唯一的客户,其会话是30分钟或更高的。
例如:从上表中,我将在5月8日
预期结果
” 。
We have an activity database that records user interaction to a website, storing a log that includes values such as Time1
, session_id
and customer_id
e.g.
2022-05-12 08:00:00|11|1
2022-05-12 08:20:00|11|1
2022-05-12 08:30:01|11|1
2022-05-12 08:14:00|22|2
2022-05-12 08:18:00|22|2
2022-05-12 08:16:00|33|1
2022-05-12 08:50:00|33|1
I need to have two separate queries:
Query #1: I need to count sessions multiple times if they have a log of 30 minutes or more grouping them on sessions on daily basis.
For example: Initially count=0
For session_id = 11, it starts at 08:00 and the last time with the same session_id is 08:30 -- count=1
For session_id = 22 it starts at 08:14 and the last time with the same session is 08:14 -- still the count=1 since it was less than 30 min
I tried this query, but it didn't work
select
count(session_id)
from
table1
where
@datetime between Time1 and dateadd(minute, 30, Time1);
Expected result:
Query #2: it's an extension of the above query where I need the unique customers on daily basis whose sessions were 30 min or more.
For example: from the above table I will have two unique customers on May 8th
Expected result
For the Time1 column, the input is in timestamp format when I show it in output I will group it on a basis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个两级聚集(组成)问题。您需要从一个子查询开始,以获取每个会话的第一个也是最后一个时间戳。
接下来,您需要使用这样的子查询:
This is a two-level aggregation (GROUP BY) problem. You need to start with a subquery to get the first and last timestamp of each session.
Next you need to use the subquery like this: