Continue Entry Numbers for a Daily Incremental Batch
Role: Analytics Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Technical Screen
## Continue Entry Numbers for a Daily Incremental Batch
The source relation is named `entries` for this exercise and has these columns:
| Column | Meaning |
|---|---|
| `entry_uuid` | Identifier of an entry. |
| `context_uuid` | Chat or conversation containing the entry. |
| `visitor_id` | Visitor associated with the entry. |
| `created_at` | Entry creation timestamp. |
| `platform` | Platform on which the entry was created. |
Here, `entries` contains only the new entries in one daily processing batch. A second table, `context_state`, records the last processed entry number for each context:
| Column | Meaning |
|---|---|
| `context_uuid` | Context identifier. |
| `max_entry_number` | Largest entry number already processed for that context. |
This is the ordinary incremental case: the new entries follow the previously processed entries. Historical-gap recovery is a separate case. For a context with no previously processed entries, the previous maximum is zero.
Write one read-only PostgreSQL query that assigns the next entry numbers to the new batch. For each context, the numbering continues immediately after its recorded `max_entry_number`. Within the batch, order entries of that context by `created_at` ascending and then `entry_uuid` ascending. A new context starts at 1.
Return only the entries in the current batch with these columns in this order:
`entry_uuid`, `context_uuid`, `visitor_id`, `created_at`, `platform`, `entry_number`.
The final result row order is unrestricted. Calculate the new numbers without modifying `entries` or `context_state`.
Overview: Continue per-context entry numbering for a daily batch using stored maximums, ordered row numbers, and correct new-context handling.
Read the full Analytics Engineer interview experience this question came from
## Continue Entry Numbers for a Daily Incremental Batch
The source relation is named `entries` for this exercise and has these columns:
| Column | Meaning |
|---|---|
| `entry_uuid` | Identifier of an entry. |
| `context_uuid` | Chat or conversation containing the entry. |
| `visitor_id` | Visitor associated with the entry. |
| `created_at` | Entry creation timestamp. |
| `platform` | Platform on which the entry was created. |
Here, `entries` contains only the new entries in one daily processing batch. A second table, `context_state`, records the last processed entry number for each context:
| Column | Meaning |
|---|---|
| `context_uuid` | Context identifier. |
| `max_entry_number` | Largest entry number already processed for that context. |
This is the ordinary incremental case: the new entries follow the previously processed entries. Historical-gap recovery is a separate case. For a context with no previously processed entries, the previous maximum is zero.
Write one read-only PostgreSQL query that assigns the next entry numbers to the new batch. For each context, the numbering continues immediately after its recorded `max_entry_number`. Within the batch, order entries of that context by `created_at` ascending and then `entry_uuid` ascending. A new context starts at 1.
Return only the entries in the current batch with these columns in this order:
`entry_uuid`, `context_uuid`, `visitor_id`, `created_at`, `platform`, `entry_number`.
The final result row order is unrestricted. Calculate the new numbers without modifying `entries` or `context_state`.
Tables
entries(entry_uuid TEXT, context_uuid TEXT, visitor_id INTEGER, created_at TIMESTAMP, platform TEXT)
context_state(context_uuid TEXT, max_entry_number BIGINT)