题目
Table: Activity
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| session_id | int |
| activity_date | date |
| activity_type | enum |
+---------------+---------+
该表没有主键,它可能有重复的行。
activity_type列是一种类型的ENUM(“ open_session”,“ end_session”,“ scroll_down”,“ send_message”)。
该表显示了社交媒体网站的用户活动。
请注意,每个会话完全属于一个用户。
编写SQL查询以查找截至2019年7月27日(含)的30天内每个用户的平均会话数,四舍五入到小数点后两位。我们要为用户计算的会话是在该时间段内至少进行了一项活动的会话。
查询结果格式如下例所示:
Activity table:
+---------+------------+---------------+---------------+
| user_id | session_id | activity_date | activity_type |
+---------+------------+---------------+---------------+
| 1 | 1 | 2019-07-20 | open_session |
| 1 | 1 | 2019-07-20 | scroll_down |
| 1 | 1 | 2019-07-20 | end_session |
| 2 | 4 | 2019-07-20 | open_session |
| 2 | 4 | 2019-07-21 | send_message |
| 2 | 4 | 2019-07-21 | end_session |
| 3 | 2 | 2019-07-21 | open_session |
| 3 | 2 | 2019-07-21 | send_message |
| 3 | 2 | 2019-07-21 | end_session |
| 3 | 5 | 2019-07-21 | open_session |
| 3 | 5 | 2019-07-21 | scroll_down |
| 3 | 5 | 2019-07-21 | end_session |
| 4 | 3 | 2019-06-25 | open_session |
| 4 | 3 | 2019-06-25 | end_session |
+---------+------------+---------------+---------------+
Result table:
+---------------------------+
| average_sessions_per_user |
+---------------------------+
| 1.33 |
+---------------------------+
User 1 和 2 在过去30天内各自进行了1次会话,而用户3进行了2次会话,因此平均值为(1 +1 + 2)/ 3 = 1.33。
解答
先选出过去30天的记录
SELECT *
FROM Activity1 AS A
WHERE A.activity_date BETWEEN '2019-06-28' AND '2019-07-27';
对uid去重计数得到总的用户个数 对sd去重计数得到总的会话次数
SELECT COUNT(DISTINCT A.`user_id`) AS num_user, COUNT(DISTINCT A.`session_id`) AS num_session
FROM Activity1 AS A
WHERE A.activity_date BETWEEN '2019-06-28' AND '2019-07-27';
两者相除即可
SELECT IFNULL(ROUND(COUNT(DISTINCT A.`session_id`)/COUNT(DISTINCT A.`user_id`), 2) , 0) AS average_sessions_per_user
FROM Activity1 AS A
WHERE A.activity_date BETWEEN '2019-06-28' AND '2019-07-27';
网友评论