cswork / libraryOpen practice workspace

Coding questions

Continue Entry Numbers for a Daily Incremental Batch

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:

ColumnMeaning
entry_uuidIdentifier of an entry.
context_uuidChat or conversation containing the entry.
visitor_idVisitor associated with the entry.
created_atEntry creation timestamp.
platformPlatform 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:

ColumnMeaning
context_uuidContext identifier.
max_entry_numberLargest 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.

Continue Entry Numbers for a Daily Incremental Batch

Table entries

columntypeconstraints
entry_uuidTEXT
context_uuidTEXT
visitor_idINTEGER
created_atTIMESTAMP
platformTEXT

Sample data:

context_uuidcreated_atentry_uuidplatformvisitor_id
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 11:00:0000000000-0000-4000-8000-000000000003web101
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 13:00:0000000000-0000-4000-8000-000000000001ios102
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 09:00:0000000000-0000-4000-8000-000000000004web101
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 11:00:0000000000-0000-4000-8000-000000000002android103
bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb2026-09-28 12:00:0000000000-0000-4000-8000-000000000006web101
bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb2026-09-28 10:00:0000000000-0000-4000-8000-000000000005web101
cccccccc-cccc-4ccc-8ccc-cccccccccccc2026-09-28 08:00:0000000000-0000-4000-8000-000000000008android201
cccccccc-cccc-4ccc-8ccc-cccccccccccc2026-09-28 08:00:0000000000-0000-4000-8000-000000000007ios202
dddddddd-dddd-4ddd-8ddd-dddddddddddd2026-09-28 10:00:0000000000-0000-4000-8000-000000000010NULL301
dddddddd-dddd-4ddd-8ddd-dddddddddddd2026-09-28 07:00:0000000000-0000-4000-8000-000000000009web301

Table context_state

columntypeconstraints
context_uuidTEXT
max_entry_numberBIGINT

Sample data:

context_uuidmax_entry_number
bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb12
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa5
dddddddd-dddd-4ddd-8ddd-dddddddddddd0
ffffffff-ffff-4fff-8fff-ffffffffffff99

Expected output

entry_uuidcontext_uuidvisitor_idcreated_atplatformentry_number
00000000-0000-4000-8000-000000000004aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa1012026-09-28 09:00:00web6
00000000-0000-4000-8000-000000000002aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa1032026-09-28 11:00:00android7
00000000-0000-4000-8000-000000000003aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa1012026-09-28 11:00:00web8
00000000-0000-4000-8000-000000000001aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa1022026-09-28 13:00:00ios9
00000000-0000-4000-8000-000000000005bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb1012026-09-28 10:00:00web13
00000000-0000-4000-8000-000000000006bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb1012026-09-28 12:00:00web14
00000000-0000-4000-8000-000000000007cccccccc-cccc-4ccc-8ccc-cccccccccccc2022026-09-28 08:00:00ios1
00000000-0000-4000-8000-000000000008cccccccc-cccc-4ccc-8ccc-cccccccccccc2012026-09-28 08:00:00android2
00000000-0000-4000-8000-000000000009dddddddd-dddd-4ddd-8ddd-dddddddddddd3012026-09-28 07:00:00web1
00000000-0000-4000-8000-000000000010dddddddd-dddd-4ddd-8ddd-dddddddddddd3012026-09-28 10:00:00NULL2