英文:
Calculate total time between earliest and latest visit for all visits
问题
以下是翻译好的部分:
"我正在存储用户的访问数据在以下表格中(包括示例数据):
id | user_id | page | visit_id | page_visit_created_at | page_visit_last_activity |
---|---|---|---|---|---|
1 | 1 | page1 | 11 | 2023-06-21 16:56:13.950 | 2023-06-21 16:58:23.823 |
2 | 2 | page1 | 12 | 2023-06-21 17:36:45.550 | 2023-06-21 17:50:23.553 |
3 | 1 | page2 | 11 | 2023-06-21 17:01:12.120 | 2023-06-21 17:04:51.122 |
4 | 3 | page1 | 14 | 2023-06-21 18:02:13.421 | 2023-06-21 18:09:11.111 |
5 | 3 | page3 | 15 | 2023-06-21 18:23:51.383 | 2023-06-21 18:31:11.347 |
6 | 1 | page1 | 16 | 2023-06-22 13:10:11.223 | 2023-06-22 13:15:10.111 |
我需要计算每个用户的总活动时间,也就是对于每个用户,需要将page_visit_created_at
和page_visit_last_activity
之间的时间总和,按visit_id
进行分组。
在上面的示例中,用户的user_id
为1的用户在同一个访问会话中访问了page1
和page2
,这就是为什么它们具有相同的visit_id
,所以我需要找到visit_id
为11的最早page_visit_created_at
,即来自第1行的2023-06-21 16:56:13.950
,以及最晚的page_visit_last_activity
,即2023-06-21 17:04:51.122
。
他还以不同的visit_id
(访问16)只访问了一次page1
,因此最早的时间是2023-06-22 13:10:11.223
,最晚的时间是2023-06-22 13:15:10.111
,因此总和应该是这两者的时间之和。
以下查询不起作用:
SELECT
user_id, visit_id,
SUM(DATEDIFF(SECOND, MIN(page_visit_created_at), MAX(page_visit_last_activity)))
FROM
my_table
GROUP BY
user_id, visit_id
我收到这个错误消息:
不能在包含聚合或子查询的表达式上执行聚合函数。
英文:
I am storing user's visits in the following table (with sample data):
id | user_id | page | visit_id | page_visit_created_at | page_visit_last_activity |
---|---|---|---|---|---|
1 | 1 | page1 | 11 | 2023-06-21 16:56:13.950 | 2023-06-21 16:58:23.823 |
2 | 2 | page1 | 12 | 2023-06-21 17:36:45.550 | 2023-06-21 17:50:23.553 |
3 | 1 | page2 | 11 | 2023-06-21 17:01:12.120 | 2023-06-21 17:04:51.122 |
4 | 3 | page1 | 14 | 2023-06-21 18:02:13.421 | 2023-06-21 18:09:11.111 |
5 | 3 | page3 | 15 | 2023-06-21 18:23:51.383 | 2023-06-21 18:31:11.347 |
6 | 1 | page1 | 16 | 2023-06-22 13:10:11.223 | 2023-06-22 13:15:10.111 |
And I need to calculate the total activity time for each user, which means to sum the time between page_visit_created_at
and page_visit_last_activity
for each user, per visit_id
.
In the example above, the user with user_id
1 visited page1
and page2
at the same visit session, that's why they have the same visit_id
, so I need to minimum page_visit_created_at
for visit_id
11, which is 2023-06-21 16:56:13.950
from row 1, and the latest page_visit_last_activity
which is 2023-06-21 17:04:51.122
.
He also visited page1
with a different visit_id
(visit 16) and once only so the minimum is 2023-06-22 13:10:11.223
and max is 2023-06-22 13:15:10.111
so the total would be the sum of the two.
The following query doesn't work:
SELECT
user_id, visit_id,
SUM(DATEDIFF(SECOND, MIN(page_visit_created_at), MAX(page_visit_last_activity)))
FROM
my_table
GROUP BY
user_id, visit_id
I get this error:
> Cannot perform an aggregate function on an expression containing an aggregate or a subquery
答案1
得分: 2
错误消息表明不允许在聚合函数内部使用聚合函数。一种选项是在子查询中分别使用 min()
和 max()
计算每次访问的花费时间,然后在这个数据集上应用聚合函数 sum()
:
SELECT user_id, SUM(time_per_visit) as total
FROM (
SELECT user_id, visit_id,
DATEDIFF(SECOND, MIN(page_visit_created_at), MAX(page_visit_last_activity)) as time_per_visit
FROM my_table
GROUP BY user_id, visit_id
) AS s
GROUP BY user_id
英文:
The error message indicates that using aggregate functions within aggregate functions is not permitted. one option is to calculate time spent per visit using min()
and max()
separately in a subquery then apply the aggregate function sum()
on this dataset :
SELECT user_id, SUM(time_per_visit) as total
FROM (
SELECT user_id, visit_id,
DATEDIFF(SECOND, MIN(page_visit_created_at), MAX(page_visit_last_activity)) as time_per_visit
FROM my_table
GROUP BY user_id, visit_id
) AS s
GROUP BY user_id
答案2
得分: 2
需要分两个阶段进行汇总。例如:
select user_id, sum(session) as total
from (
select
user_id, visit_id,
datediff(second, min(page_visit_created_at),
max(page_visit_last_activity)) as session
from t
group by user_id, visit_id
) x
group by user_id
英文:
You need to aggregate in two phases. For example:
select user_id, sum(session) as total
from (
select
user_id, visit_id,
datediff(second, min(page_visit_created_at),
max(page_visit_last_activity)) as session
from t
group by user_id, visit_id
) x
group by user_id
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论