runtime
31 / 88

task ROW_NUMBER · medium

First order versus latest order

For shoppers with at least two non-cancelled orders, return shopper_id, first_order_value and latest_order_value. An order's value is the sum of its lines' quantity × unit_price.

what to return

Columns
shopper_idfirst_order_valuelatest_order_value in this order
Rows
10 rows · any order
Watch for
“First” and “latest” go by order_date; on the same date, the higher order id is later.
Round values to 2 decimals.
Skill
Pick first and last rows per group, not minimum and maximum values. Refresher: FIRST_VALUE, LAST_VALUE & frames →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result10 rows · any order
shopper_idfirst_order_valuelatest_order_value
178118
25948
3149108.5

first 3 of 10 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

sales_orders5 columns · 33 rows
idinteger · keyshopper_idinteger → shoppersorder_datetextstatustextdelivered_datetext
10112025-01-04delivered2025-01-08
10222025-01-06delivered2025-01-07
10312025-01-15delivered2025-01-20
10432025-01-18cancelledNULL
10542025-01-22delivered2025-01-31

showing 5 of 33 rows

order_lines4 columns · 48 rows
order_idinteger → sales_ordersitem_idinteger → catalog_itemsquantityintegerunit_pricereal
1011139
1013219.5
1024159
1035145
10310312

showing 5 of 48 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.