#227
Seattle Shipped Orders Total Value
MEDIUMJoins & AggregationPOSTGRESQL
Problem Description
You are building a **regional sales reporting pipeline** for an e-commerce platform. The platform tracks customers, their orders, and shipment fulfillment records across multiple cities.
Your task is to calculate the **total value of shipped orders** placed by customers in Seattle within the last 12 months.
**Tables:**
- `customer`: Customer profiles, including their `city`
- `orders`: Order line items with `item_price`, `quantity`, `cust_id`, `shipment_id`, and `order_date`
- `shipments`: Fulfillment records linking orders to shipment events
**Key Rules:**
- Only include customers where `city = 'Seattle'`
- Only include orders where `order_date >= CURRENT_DATE - INTERVAL '12 months'`
- An order is considered **shipped** if its `shipment_id` is NOT NULL (i.e., it appears in the shipments table)
- Order value = `item_price * quantity` per line item
- If no qualifying orders exist, return `0.00` (not NULL)
**Output:** Return a single row with column `total_value` — the sum of `item_price * quantity` for all qualifying orders.
**Important:** Use `today()` instead of `CURRENT_DATE` for all date calculations in your query. This function is pre-defined in the test environment and returns a fixed reference date that the test data is built around.
Topics
aggregation
Asked at Companies
amazon
Solve This Problem
Sign up to access the interactive code editor, run test cases, view the editorial, and get AI-powered feedback on your solution.
Start Solving →