Number Entries Within Each Conversation
OtherAnalytics Engineer · Technical ScreenLast updated
Assign one-based entry numbers per conversation using timestamp and identifier ordering in a PostgreSQL window-function query.
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. |
A conversation can contain several entries. Write one read-only PostgreSQL query that assigns each entry its position within its own context_uuid.
Start numbering at 1 for each context. Within a context, order entries by created_at ascending and then by entry_uuid ascending. The entry identifier resolves a tie in creation timestamps.
Return the original columns followed by the new entry_number column, in this order:
entry_uuid, context_uuid, visitor_id, created_at, platform, entry_number.
The required order determines the numbering within each context. The final result rows may be returned in any order.
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.