Seven-Day Ridership Trend
Problem
Table: `Tap`
```text
+------------+---------+
| Column | Type |
+------------+---------+
| rider_id | int |
| rider_name | varchar |
| tapped_on | date |
| fare_cents | int |
+------------+---------+
(rider_id, tapped_on) is the primary key. Each row is one fare a rider paid
when tapping into a transit station on a given day. There is at least one tap
every day.
```
You run a transit network and want a smoothed view of revenue. For every day that has **six full days before it** in the data, compute:
- `day_total` — the total fares collected over the seven-day window ending on that day (the day itself plus the six prior days),
- `window_average` — `day_total` divided by 7, rounded to two decimal places.
Return `tapped_on`, `day_total`, and `window_average`, ordered by `tapped_on`. The earliest six days of the dataset never have a full window and are omitted.
**Example**
```text
For seven consecutive days with daily totals 100, 100, 100, 100, 100, 100, 100
the first qualifying day is the seventh, with day_total = 700 and
window_average = 100.00.
```
Tables
Example rows — the live problem includes the full dataset.
| rider_id | rider_name | tapped_on | fare_cents |
|---|---|---|---|
| 1 | Mara | 2023-01-01 | 100 |
| 2 | Theo | 2023-01-02 | 100 |
| 3 | Lena | 2023-01-03 | 100 |
Expected output
Your answer should return 2 rows with the columns tapped_on, day_total, window_average.
Starter code (SQL)
SELECT *
FROM Tap;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock