我正在尝试选择保留在某个日期范围内的用户数,并且使用此查询和下表成功:
+----------+-------------+
| Field | Type |
+----------+-------------+
| id | varchar(17) |
| log_date | date |
+----------+-------------+
SELECT last_day.log_date, COUNT(distinct last_day.id) as users_num
FROM (SELECT DISTINCT log_date, id
FROM `userActivity`) this_day
JOIN (SELECT DISTINCT log_date, id
FROM `userActivity`) last_day
ON this_day.id = last_day.id
AND this_day.log_date = "2018-10-01"
AND last_day.log_date BETWEEN "2018-10-01" AND "2018-10-30"
GROUP BY log_date;
但我面临的问题是我希望假设日期范围的每一天都是第 0 天。(类似于以下示例):
请注意,图片中的第一行是我需要计算的以下结果的平均值。任何人都知道如何增强我的查询以获得像图片一样的结果?