task Views, temp tables & triggers · medium
A reusable shopper totals view
Create a view shopper_order_totals with columns shopper_id, orders (number of non-cancelled orders) and total_spend (sum of quantity × unit_price over those orders, 2 decimals). Then return the view's rows where total_spend is above 150.
what to return
- Columns
shopper_idorderstotal_spendin this order- Rows
- 7 rows · any order
- Watch for
- orders counts orders, not order lines.
- Only shoppers with at least one non-cancelled order appear in the view.
- Skill
- Encapsulate a correct aggregate in a view. Refresher: Readable queries with CTEs →
expected result
Your query should return exactly these columns and values. Run it and compare.
expected result7 rows · any order
| shopper_id | orders | total_spend |
|---|---|---|
| 1 | 5 | 367 |
| 2 | 4 | 270 |
| 3 | 2 | 257.5 |
first 3 of 7 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 · key | shopper_idinteger → shoppers | order_datetext | statustext | delivered_datetext |
|---|---|---|---|---|
| 101 | 1 | 2025-01-04 | delivered | 2025-01-08 |
| 102 | 2 | 2025-01-06 | delivered | 2025-01-07 |
| 103 | 1 | 2025-01-15 | delivered | 2025-01-20 |
| 104 | 3 | 2025-01-18 | cancelled | NULL |
| 105 | 4 | 2025-01-22 | delivered | 2025-01-31 |
showing 5 of 33 rows
order_lines4 columns · 48 rows
| order_idinteger → sales_orders | item_idinteger → catalog_items | quantityinteger | unit_pricereal |
|---|---|---|---|
| 101 | 1 | 1 | 39 |
| 101 | 3 | 2 | 19.5 |
| 102 | 4 | 1 | 59 |
| 103 | 5 | 1 | 45 |
| 103 | 10 | 3 | 12 |
showing 5 of 48 rows
solution.sql
Hints and the answer are always available. Using them counts the question as solved with help.