-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2854-RollingAverageSteps.sql
More file actions
119 lines (112 loc) · 5.91 KB
/
2854-RollingAverageSteps.sql
File metadata and controls
119 lines (112 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- 2854. Rolling Average Steps
-- Table: Steps
-- +-------------+------+
-- | Column Name | Type |
-- +-------------+------+
-- | user_id | int |
-- | steps_count | int |
-- | steps_date | date |
-- +-------------+------+
-- (user_id, steps_date) is the primary key for this table.
-- Each row of this table contains user_id, steps_count, and steps_date.
-- Write a solution to calculate 3-day rolling averages of steps for each user.
-- We calculate the n-day rolling average this way:
-- For each day, we calculate the average of n consecutive days of step counts ending on that day if available, otherwise, n-day rolling average is not defined for it.
-- Output the user_id, steps_date, and rolling average. Round the rolling average to two decimal places.
-- Return the result table ordered by user_id, steps_date in ascending order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Steps table:
-- +---------+-------------+------------+
-- | user_id | steps_count | steps_date |
-- +---------+-------------+------------+
-- | 1 | 687 | 2021-09-02 |
-- | 1 | 395 | 2021-09-04 |
-- | 1 | 499 | 2021-09-05 |
-- | 1 | 712 | 2021-09-06 |
-- | 1 | 576 | 2021-09-07 |
-- | 2 | 153 | 2021-09-06 |
-- | 2 | 171 | 2021-09-07 |
-- | 2 | 530 | 2021-09-08 |
-- | 3 | 945 | 2021-09-04 |
-- | 3 | 120 | 2021-09-07 |
-- | 3 | 557 | 2021-09-08 |
-- | 3 | 840 | 2021-09-09 |
-- | 3 | 627 | 2021-09-10 |
-- | 5 | 382 | 2021-09-05 |
-- | 6 | 480 | 2021-09-01 |
-- | 6 | 191 | 2021-09-02 |
-- | 6 | 303 | 2021-09-05 |
-- +---------+-------------+------------+
-- Output:
-- +---------+------------+-----------------+
-- | user_id | steps_date | rolling_average |
-- +---------+------------+-----------------+
-- | 1 | 2021-09-06 | 535.33 |
-- | 1 | 2021-09-07 | 595.67 |
-- | 2 | 2021-09-08 | 284.67 |
-- | 3 | 2021-09-09 | 505.67 |
-- | 3 | 2021-09-10 | 674.67 |
-- +---------+------------+-----------------+
-- Explanation:
-- - For user id 1, the step counts for the three consecutive days up to 2021-09-06 are available. Consequently, the rolling average for this particular date is computed as (395 + 499 + 712) / 3 = 535.33.
-- - For user id 1, the step counts for the three consecutive days up to 2021-09-07 are available. Consequently, the rolling average for this particular date is computed as (499 + 712 + 576) / 3 = 595.67.
-- - For user id 2, the step counts for the three consecutive days up to 2021-09-08 are available. Consequently, the rolling average for this particular date is computed as (153 + 171 + 530) / 3 = 284.67.
-- - For user id 3, the step counts for the three consecutive days up to 2021-09-09 are available. Consequently, the rolling average for this particular date is computed as (120 + 557 + 840) / 3 = 505.67.
-- - For user id 3, the step counts for the three consecutive days up to 2021-09-10 are available. Consequently, the rolling average for this particular date is computed as (557 + 840 + 627) / 3 = 674.67.
-- - For user id 4 and 5, the calculation of the rolling average is not viable as there is insufficient data for the consecutive three days. Output table ordered by user_id and steps_date in ascending order.
-- Create table if not exists Steps(user_id int, steps_count int, steps_date date)
-- Truncate table Steps
-- insert into Steps (user_id, steps_count, steps_date) values ('1', '687', '2021-09-02')
-- insert into Steps (user_id, steps_count, steps_date) values ('1', '395', '2021-09-04')
-- insert into Steps (user_id, steps_count, steps_date) values ('1', '499', '2021-09-05')
-- insert into Steps (user_id, steps_count, steps_date) values ('1', '712', '2021-09-06')
-- insert into Steps (user_id, steps_count, steps_date) values ('1', '576', '2021-09-07')
-- insert into Steps (user_id, steps_count, steps_date) values ('2', '153', '2021-09-06')
-- insert into Steps (user_id, steps_count, steps_date) values ('2', '171', '2021-09-07')
-- insert into Steps (user_id, steps_count, steps_date) values ('2', '530', '2021-09-08')
-- insert into Steps (user_id, steps_count, steps_date) values ('3', '945', '2021-09-04')
-- insert into Steps (user_id, steps_count, steps_date) values ('3', '120', '2021-09-07')
-- insert into Steps (user_id, steps_count, steps_date) values ('3', '557', '2021-09-08')
-- insert into Steps (user_id, steps_count, steps_date) values ('3', '840', '2021-09-09')
-- insert into Steps (user_id, steps_count, steps_date) values ('3', '627', '2021-09-10')
-- insert into Steps (user_id, steps_count, steps_date) values ('5', '382', '2021-09-05')
-- insert into Steps (user_id, steps_count, steps_date) values ('6', '480', '2021-09-01')
-- insert into Steps (user_id, steps_count, steps_date) values ('6', '191', '2021-09-02')
-- insert into Steps (user_id, steps_count, steps_date) values ('6', '303', '2021-09-05')
# Write your MySQL query statement below
-- SELECT
-- a.*,
-- b.*,
-- c.*
-- FROM
-- Steps AS a
-- LEFT JOIN
-- Steps AS b
-- ON
-- a.user_id = b.user_id AND DATEDIFF(a.steps_date,b.steps_date) = 1
-- LEFT JOIN
-- Steps AS c
-- ON
-- a.user_id = c.user_id AND DATEDIFF(a.steps_date,c.steps_date) = 2
-- WHERE
-- b.user_id IS NOT NULL AND c.user_id IS NOT NULL
SELECT
a.user_id,
a.steps_date,
ROUND((a.steps_count + b.steps_count + c.steps_count) / 3,2) AS rolling_average
FROM
Steps AS a
LEFT JOIN
Steps AS b
ON
a.user_id = b.user_id AND DATEDIFF(a.steps_date,b.steps_date) = 1
LEFT JOIN
Steps AS c
ON
a.user_id = c.user_id AND DATEDIFF(a.steps_date,c.steps_date) = 2
WHERE
b.user_id IS NOT NULL AND c.user_id IS NOT NULL
ORDER BY
user_id, steps_date -- 返回结果表以user_id 和 steps_date 升序 排序。