Latest Order per Customer
Part of the Connected Data section of Coddy's PostgreSQL journey. Lesson 15 of 25.
Challenge
MediumReturn customer_id, order_id and amount for each customer latest order by date, using greater id to break equal-date ties. Use a window calculation inside a CTE and filter in the outer query. Order by customer_id.
The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.
Try it yourself
-- Write your SQL here.
All lessons in Connected Data
3Window Calculations
Number Rows in GroupsRanks and TiesRunning TotalsCompare with LAGLatest Order per CustomerPractice on your own: PostgreSQL playground