Continue Entry Numbers for a Daily Incremental Batch
OtherAnalytics Engineer · Technical ScreenLast updated
Continue per-context entry numbering for a daily batch using stored maximums, ordered row numbers, and correct new-context handling.
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.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.