现有一张用户观看记录表user fs view,基于该表需要进行相关统计分析 观看日期 时日 ymd user_id age view_time register_time 20200401 25 15min 2020/4/1 13:01 20200401 2 33 24min 2020/4/1 15:06 20200402 1 25 35min 2020/4/1 13:01 20200403 4 38 4min 2020/4/321:00 20200404 1 15 9min 2020/4/1 13:01 20200402 2 33 33min 2020/4/1 15:06 20200403 2 33 17min 2020/4/1 15:06 20200402 3 3 45 47min 2020/4/2 11:00 20200404 5 55 27min 2020/4/4 21:00 20200405 5 55 24min 2020/4/4 21:00 问题如下 1.统计:用户总量,用户平均年龄 2.统计:每天注册的用户数.和用户累计观看时长,用户平均观看时长 3.统计:每10岁一个分段,统计每个区间的用户总量,用户平均观看时长 4.统计:每天观看时长最长的用户userid 5.统计:用户次日留存情况 6.统计:每天观看时长都大于20min的用户总量,只要有一天观看时长小于20min就不算。 解题要求 请基于MySOL数据库用sg1语句分别写出以上问题的解答SQL
时间: 2023-12-14 19:40:21 浏览: 137
1. 统计用户总量和用户平均年龄
```
-- 用户总量
SELECT COUNT(DISTINCT user_id) AS user_count FROM user_fs_view;
-- 用户平均年龄
SELECT AVG(age) AS avg_age FROM user_fs_view;
```
2. 统计每天注册的用户数、用户累计观看时长和用户平均观看时长
```
-- 每天注册的用户数
SELECT register_time, COUNT(DISTINCT user_id) AS user_count
FROM user_fs_view
GROUP BY register_time;
-- 用户累计观看时长和平均观看时长
SELECT user_id, SUM(view_time) AS total_view_time, AVG(view_time) AS avg_view_time
FROM user_fs_view
GROUP BY user_id;
```
3. 统计每个10岁分段的用户总量和平均观看时长
```
-- 每个10岁分段的用户总量和平均观看时长
SELECT FLOOR(age/10)*10 AS age_range, COUNT(DISTINCT user_id) AS user_count, AVG(view_time) AS avg_view_time
FROM user_fs_view
GROUP BY age_range;
```
4. 统计每天观看时长最长的用户userid
```
-- 每天观看时长最长的用户userid
SELECT user_id, view_time, ymd
FROM user_fs_view
WHERE (ymd, view_time) IN (
SELECT ymd, MAX(view_time) AS max_view_time
FROM user_fs_view
GROUP BY ymd
);
```
5. 统计用户次日留存情况
```
-- 用户次日留存情况
SELECT COUNT(DISTINCT user_id) AS retention_count
FROM user_fs_view AS a
WHERE EXISTS (
SELECT 1
FROM user_fs_view AS b
WHERE a.user_id = b.user_id AND DATEDIFF(b.ymd, a.ymd) = 1
);
```
6. 统计每天观看时长都大于20min的用户总量
```
-- 每天观看时长都大于20min的用户总量
SELECT COUNT(DISTINCT user_id) AS user_count
FROM user_fs_view
WHERE user_id NOT IN (
SELECT user_id
FROM user_fs_view
WHERE view_time < 20
);
```
阅读全文