Write a SQL query that returns each customer's total data_usage_mb for each month of 2024. Only usage dated in 2024 counts. Each output row is one customer in one month, with the customer's name and the total usage rounded to 2 decimals.
Input Table(s)
customers
| Column | Type | Meaning |
|---|
customer_id | INT | Primary key |
customer_name | VARCHAR(100) | Customer name, never NULL |
traffic_usage
| Column | Type | Meaning |
|---|
usage_id | INT | Primary key, one row per usage record |
customer_id | INT | Foreign key to customers.customer_id |
usage_date | DATE | Date of the usage, never NULL |
data_usage_mb | DECIMAL(10,2) | Data used in megabytes, never NULL and never negative |
A customer can have zero, one or many usage records in a month.
Output Contract
| Column | Type | Meaning |
|---|
customer_id | INTEGER | The customer |
customer_name | TEXT | Name from customers |
Year | INTEGER | Calendar year of the usage, always 2024 |
Month | INTEGER | Calendar month of the usage, 1 to 12 |
TotalUsageMB | DECIMAL | Sum of data_usage_mb for that customer and month, rounded to 2 decimals |
Column names are case-sensitive and must match exactly. Order by customer_id ascending, then Month ascending. Each (customer_id, Month) pair appears once, so this order is fully determined.
Example
Input customers:
| customer_id | customer_name |
|---|
| 1 | Acme Corp |
| 2 | Blue Fin Labs |
| 3 | Cedar Health |
Input traffic_usage:
| usage_id | customer_id | usage_date | data_usage_mb |
|---|
| 1 | 1 | 2024-01-05 | 120.50 |
| 2 | 1 | 2024-01-20 | 79.50 |
| 3 | 1 | 2024-03-02 | 300.00 |
| 4 | 2 | 2023-12-31 | 500.00 |
| 5 | 2 | 2024-01-01 | 10.25 |
| 6 | 2 | 2025-01-01 | 42.00 |
| 7 | 3 | 2023-06-15 | 88.00 |
Expected output:
| customer_id | customer_name | Year | Month | TotalUsageMB |
|---|
| 1 | Acme Corp | 2024 | 1 | 200.00 |
| 1 | Acme Corp | 2024 | 3 | 300.00 |
| 2 | Blue Fin Labs | 2024 | 1 | 10.25 |
Acme's two January records add to 120.50 + 79.50 = 200.00, and Acme has no February row because it had no February usage. Blue Fin Labs' records on 2023-12-31 and 2025-01-01 fall outside 2024. Cedar Health has no 2024 usage, so it does not appear.
Constraints and Clarifications
-
customers
has at most 10,000 rows and
traffic_usage
at most 100,000 rows.
-
usage_date
holds valid dates spanning several years; only 2024 dates count.
-
data_usage_mb
is non-negative.
-
Report only (customer, month) pairs with at least one 2024 usage record. Do not output zero rows for months without usage, and do not list customers with no 2024 usage.
-
Usage rows whose
customer_id
does not match a customer are not reported.