|
| 1 | +# [2854. Rolling Average Steps](https://leetcode.cn/problems/rolling-average-steps) |
| 2 | + |
| 3 | +[English Version](/solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code><font face="monospace">Steps</font></code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++-------------+------+ |
| 13 | +| Column Name | Type | |
| 14 | ++-------------+------+ |
| 15 | +| user_id | int | |
| 16 | +| steps_count | int | |
| 17 | +| steps_date | date | |
| 18 | ++-------------+------+ |
| 19 | +(user_id, steps_date) is the primary key for this table. |
| 20 | +Each row of this table contains user_id, steps_count, and steps_date. |
| 21 | +</pre> |
| 22 | + |
| 23 | +<p>Write a solution to calculate <code>3-day</code> <strong>rolling averages</strong> of steps for each user.</p> |
| 24 | + |
| 25 | +<p>We calculate the <code>n-day</code> <strong>rolling average</strong> this way:</p> |
| 26 | + |
| 27 | +<ul> |
| 28 | + <li>For each day, we calculate the average of <code>n</code> consecutive days of step counts ending on that day if available, otherwise, <code>n-day</code> rolling average is not defined for it.</li> |
| 29 | +</ul> |
| 30 | + |
| 31 | +<p>Output the <code>user_id</code>, <code>steps_date</code>, and rolling average. Round the rolling average to <strong>two decimal places</strong>.</p> |
| 32 | + |
| 33 | +<p>Return<em> the result table ordered by </em><code>user_id</code><em>, </em><code>steps_date</code><em> in <strong>ascending</strong> order.</em></p> |
| 34 | + |
| 35 | +<p>The result format is in the following example.</p> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | +<p><strong class="example">Example 1:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>Input:</strong> |
| 42 | +Steps table: |
| 43 | ++---------+-------------+------------+ |
| 44 | +| user_id | steps_count | steps_date | |
| 45 | ++---------+-------------+------------+ |
| 46 | +| 1 | 687 | 2021-09-02 | |
| 47 | +| 1 | 395 | 2021-09-04 | |
| 48 | +| 1 | 499 | 2021-09-05 | |
| 49 | +| 1 | 712 | 2021-09-06 | |
| 50 | +| 1 | 576 | 2021-09-07 | |
| 51 | +| 2 | 153 | 2021-09-06 | |
| 52 | +| 2 | 171 | 2021-09-07 | |
| 53 | +| 2 | 530 | 2021-09-08 | |
| 54 | +| 3 | 945 | 2021-09-04 | |
| 55 | +| 3 | 120 | 2021-09-07 | |
| 56 | +| 3 | 557 | 2021-09-08 | |
| 57 | +| 3 | 840 | 2021-09-09 | |
| 58 | +| 3 | 627 | 2021-09-10 | |
| 59 | +| 5 | 382 | 2021-09-05 | |
| 60 | +| 6 | 480 | 2021-09-01 | |
| 61 | +| 6 | 191 | 2021-09-02 | |
| 62 | +| 6 | 303 | 2021-09-05 | |
| 63 | ++---------+-------------+------------+ |
| 64 | +<strong>Output:</strong> |
| 65 | ++---------+------------+-----------------+ |
| 66 | +| user_id | steps_date | rolling_average | |
| 67 | ++---------+------------+-----------------+ |
| 68 | +| 1 | 2021-09-06 | 535.33 | |
| 69 | +| 1 | 2021-09-07 | 595.67 | |
| 70 | +| 2 | 2021-09-08 | 284.67 | |
| 71 | +| 3 | 2021-09-09 | 505.67 | |
| 72 | +| 3 | 2021-09-10 | 674.67 | |
| 73 | ++---------+------------+-----------------+ |
| 74 | +<strong>Explanation:</strong> |
| 75 | +- 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. |
| 76 | +- 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. |
| 77 | +- 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. |
| 78 | +- 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. |
| 79 | +- 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. |
| 80 | +- 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.</pre> |
| 81 | + |
| 82 | +## 解法 |
| 83 | + |
| 84 | +<!-- 这里可写通用的实现逻辑 --> |
| 85 | + |
| 86 | +**方法一:窗口函数** |
| 87 | + |
| 88 | +我们用窗口函数 `LAG() OVER()` 来计算每个用户当前日期与上上个日期之间的天数差,如果为 $2$,说明这两个日期之间有连续 $3$ 天的数据,我们可以利用窗口函数 `AVG() OVER()` 来计算这 $3$ 个数据的平均值。 |
| 89 | + |
| 90 | +<!-- tabs:start --> |
| 91 | + |
| 92 | +### **SQL** |
| 93 | + |
| 94 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 95 | + |
| 96 | +```sql |
| 97 | +# Write your MySQL query statement below |
| 98 | +WITH |
| 99 | + T AS ( |
| 100 | + SELECT |
| 101 | + user_id, |
| 102 | + steps_date, |
| 103 | + round( |
| 104 | + avg(steps_count) OVER ( |
| 105 | + PARTITION BY user_id |
| 106 | + ORDER BY steps_date |
| 107 | + ROWS 2 PRECEDING |
| 108 | + ), |
| 109 | + 2 |
| 110 | + ) AS rolling_average, |
| 111 | + datediff( |
| 112 | + steps_date, |
| 113 | + lag(steps_date, 2) OVER ( |
| 114 | + PARTITION BY user_id |
| 115 | + ORDER BY steps_date |
| 116 | + ) |
| 117 | + ) = 2 AS st |
| 118 | + FROM Steps |
| 119 | + ) |
| 120 | +SELECT |
| 121 | + user_id, |
| 122 | + steps_date, |
| 123 | + rolling_average |
| 124 | +FROM T |
| 125 | +WHERE st = 1 |
| 126 | +ORDER BY 1, 2; |
| 127 | +``` |
| 128 | + |
| 129 | +<!-- tabs:end --> |
0 commit comments