Count log entries per server per day
Company: Splunk
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Table `log_data` holds one row per log entry written by a server. Write a query that counts the entries each server wrote on each calendar day. Return `server_id`, the date as `log_date`, and the count as `daily_log_count`.
```hint Pick the grouping key
Two entries from the same server on the same date belong in one group even when their times differ.
```
### Input Table(s)
Table `log_data`:
| Column | Type | Meaning |
|---|---|---|
| log_id | INT | Unique ID of the log entry |
| server_id | INT | Server that wrote the entry, never NULL |
| log_timestamp | TIMESTAMP | Date and time the entry was written, no time zone, never NULL |
| log_level | VARCHAR(20) | One of 'INFO', 'WARN', 'ERROR', 'DEBUG', never NULL |
| message | VARCHAR(255) | Log text, may be NULL |
Primary key: `log_id`.
### Output Contract
- Columns in this order: `server_id`, `log_date` (a DATE), `daily_log_count` (an integer).
- One row per (`server_id`, `log_date`) pair that has at least one log entry.
- Rows are ordered by `server_id` ascending, then `log_date` ascending. That pair is unique per row, so the order is fully determined.
- No rounding is involved.
### Example
Input `log_data`:
| log_id | server_id | log_timestamp | log_level | message |
|---|---|---|---|---|
| 1 | 1 | 2024-05-01 08:15:00 | INFO | Service started |
| 2 | 1 | 2024-05-01 23:59:59 | ERROR | Disk quota exceeded |
| 3 | 2 | 2024-05-01 12:00:00 | WARN | High latency |
| 4 | 1 | 2024-05-02 00:00:00 | INFO | Heartbeat |
| 5 | 2 | 2024-05-03 09:30:00 | DEBUG | NULL |
| 6 | 2 | 2024-05-03 17:45:00 | INFO | Cache refreshed |
Expected output:
| server_id | log_date | daily_log_count |
|---|---|---|
| 1 | 2024-05-01 | 2 |
| 1 | 2024-05-02 | 1 |
| 2 | 2024-05-01 | 1 |
| 2 | 2024-05-03 | 2 |
Server 1 wrote two entries on 2024-05-01 and one at midnight that starts 2024-05-02. Server 2 wrote one entry on 2024-05-01 and two on 2024-05-03, one of them with a NULL message. Server 2 wrote nothing on 2024-05-02, so that date does not appear for it.
### Constraints and Clarifications
- `log_data` has 1 to 10,000 rows.
- `server_id` is a positive integer.
- Use the calendar date of `log_timestamp` as stored. No time zone conversion applies.
- An entry at exactly 00:00:00 belongs to the date that starts at that moment.
- Count every entry whatever its `log_level`, including entries whose `message` is NULL.
- Dates on which a server wrote no entries are left out. Do not fill them with zero.
Overview: Write a SQL query that counts log entries per server per calendar day from a table of timestamped logs, ordered by server and date. It tests converting timestamps to dates, grouping on two keys, and choosing the right form of COUNT.