Average daily data ingestion per client per month in SQL
Quick Overview
Write a SQL query that reports, for each client and calendar month, the average data volume ingested per active day. It tests grouping by a formatted month that keeps the year, counting distinct days when a day has several records, and rounding the result.
Average daily data ingestion per client per month in SQL
Company: Splunk
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A log-ingestion service records every batch of data it receives from each client, and a client can send several batches on the same day. For each client and calendar month, report the average volume ingested per active day: the total `data_volume_mb` for that month divided by the number of distinct days in that month on which the client has at least one record, rounded to 2 decimal places.
### Input Table(s)
`clients`
| Column | Type | Meaning |
|---|---|---|
| client_id | INT | Client identifier. Primary key. |
| client_name | VARCHAR(100) | Client display name. NOT NULL. |
`ingestion_logs`
| Column | Type | Meaning |
|---|---|---|
| log_id | INT | Log record identifier. Primary key. |
| client_id | INT | Client that sent the batch. Foreign key to `clients.client_id`. |
| ingestion_date | DATE | Day the batch was ingested. NOT NULL. |
| data_volume_mb | DECIMAL(12,2) | Size of the batch in MB. NOT NULL. |
Keys: `clients.client_id` and `ingestion_logs.log_id` are primary keys; `ingestion_logs.client_id` references `clients.client_id`.
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| client_name | VARCHAR | The client's name. |
| month | TEXT | Calendar month in `YYYY-MM` form, for example `2024-01`. |
| avg_daily_ingested_mb | NUMERIC | `SUM(data_volume_mb)` for the client and month divided by the number of distinct `ingestion_date` values in that group, rounded to 2 decimal places. |
Return one row per client and month in which that client has at least one log record. Order by `client_name` ascending, then `month` ascending, then `client_id` ascending. The last key only decides the order when two clients share a name; it is not an output column.
### Example
`clients`
| client_id | client_name |
|---|---|
| 1 | Acme |
| 2 | Beta Labs |
| 3 | Corex |
`ingestion_logs`
| log_id | client_id | ingestion_date | data_volume_mb |
|---|---|---|---|
| 1 | 1 | 2024-01-05 | 100.00 |
| 2 | 1 | 2024-01-05 | 50.00 |
| 3 | 1 | 2024-01-20 | 30.00 |
| 4 | 1 | 2024-02-02 | 80.50 |
| 5 | 2 | 2024-01-10 | 10.00 |
| 6 | 2 | 2024-01-11 | 20.00 |
| 7 | 2 | 2024-01-12 | 25.00 |
Expected result:
| client_name | month | avg_daily_ingested_mb |
|---|---|---|
| Acme | 2024-01 | 90.00 |
| Acme | 2024-02 | 80.50 |
| Beta Labs | 2024-01 | 18.33 |
Acme ingested 180.00 MB in January 2024 over two distinct days (January 5 has two records), so the average is 90.00. Beta Labs ingested 55.00 MB over three days, which is 18.333... and rounds to 18.33. Corex has no records, so it does not appear.
### Constraints and Clarifications
- `clients` has at most 100 rows and `ingestion_logs` has at most 10,000 rows.
- `data_volume_mb` is always positive and never NULL, and `ingestion_date` is never NULL.
- Every `client_id` in `ingestion_logs` exists in `clients`.
- A client may have several records on the same date. All of their volume counts toward the numerator, and that date counts once in the denominator.
- Months are calendar months, and the same month in different years are separate groups (`2023-01` and `2024-01` are two rows).
- Clients with no records, and months in which a client has no records, produce no rows. Do not output zero rows for them.
- `client_name` is not guaranteed to be unique. Two clients with the same name are still separate clients.
```hint Pick the grouping and the denominator carefully
Group by a month key that keeps the year. Then check what your denominator counts on a day that has two records.
```
Overview: Write a SQL query that reports, for each client and calendar month, the average data volume ingested per active day. It tests grouping by a formatted month that keeps the year, counting distinct days when a day has several records, and rounding the result.
A log-ingestion service records every batch of data it receives from each client, and a client can send several batches on the same day. For each client and calendar month, report the average volume ingested per active day: the total data_volume_mb for that month divided by the number of distinct days in that month on which the client has at least one record, rounded to 2 decimal places.
Input Table(s)
clients
Column
Type
Meaning
client_id
INT
Client identifier. Primary key.
client_name
VARCHAR(100)
Client display name. NOT NULL.
ingestion_logs
Column
Type
Meaning
log_id
INT
Log record identifier. Primary key.
client_id
INT
Client that sent the batch. Foreign key to clients.client_id.
ingestion_date
DATE
Day the batch was ingested. NOT NULL.
data_volume_mb
DECIMAL(12,2)
Size of the batch in MB. NOT NULL.
Keys: clients.client_id and ingestion_logs.log_id are primary keys; ingestion_logs.client_id references clients.client_id.
Output Contract
Column
Type
Meaning
client_name
VARCHAR
The client's name.
month
TEXT
Calendar month in YYYY-MM form, for example 2024-01.
avg_daily_ingested_mb
NUMERIC
SUM(data_volume_mb) for the client and month divided by the number of distinct ingestion_date values in that group, rounded to 2 decimal places.
Return one row per client and month in which that client has at least one log record. Order by client_name ascending, then month ascending, then client_id ascending. The last key only decides the order when two clients share a name; it is not an output column.
Example
clients
client_id
client_name
1
Acme
2
Beta Labs
3
Corex
ingestion_logs
log_id
client_id
ingestion_date
data_volume_mb
1
1
2024-01-05
100.00
2
1
2024-01-05
50.00
3
1
2024-01-20
30.00
4
1
2024-02-02
80.50
5
2
2024-01-10
10.00
6
2
2024-01-11
20.00
7
2
2024-01-12
25.00
Expected result:
client_name
month
avg_daily_ingested_mb
Acme
2024-01
90.00
Acme
2024-02
80.50
Beta Labs
2024-01
18.33
Acme ingested 180.00 MB in January 2024 over two distinct days (January 5 has two records), so the average is 90.00. Beta Labs ingested 55.00 MB over three days, which is 18.333... and rounds to 18.33. Corex has no records, so it does not appear.
Constraints and Clarifications
clients
has at most 100 rows and
ingestion_logs
has at most 10,000 rows.
data_volume_mb
is always positive and never NULL, and
ingestion_date
is never NULL.
Every
client_id
in
ingestion_logs
exists in
clients
.
A client may have several records on the same date. All of their volume counts toward the numerator, and that date counts once in the denominator.
Months are calendar months, and the same month in different years are separate groups (
2023-01
and
2024-01
are two rows).
Clients with no records, and months in which a client has no records, produce no rows. Do not output zero rows for them.
client_name
is not guaranteed to be unique. Two clients with the same name are still separate clients.