Latest Row per Group with DISTINCT ON
Hard About 25 min Katabench Pro
Use Postgres DISTINCT ON to keep one row per group: the most recent order per customer.
The full brief is part of Katabench Pro
Pro opens this kata's brief, its starter code and its grader. To see how the track works first, try Customer Order Totals, which is free.
How it is graded
Your LINQ or SQL runs against real PostgreSQL. You see the SQL your code generates on the sample tests and can inspect its execution plan, so you learn when the database did the wrong work.
What it practices
- Top N per group
- Pick the latest or best rows per group, with explicit tie handling and deduplication.
Related katas
- Three Leaders in Every Market Hard Pro Take a bounded set of revenue leaders inside every country after a four-table aggregate.
- The Order Digest Tree Hard Pro Shape a bounded customer, order, and line-item tree inside one translated EF Core query instead of loading the full graph.
- The Touch Must Precede This Conversion Hard Pro Attribute each conversion to its last eligible campaign touch using a conversion-relative window and deterministic tie break.
- Three Latest Orders, Every Customer Hard Pro Take the top three rows per group with CROSS JOIN LATERAL, the index-friendly sibling of the window-function rank.