You're a data analyst for a convenience store chain. The operations director is preparing a weekly summary of online orders and wants to see how many orders each store received, how many were delivered, how many were canceled, and how much revenue came from delivered orders only. The data is in the orders table, with one row per order. Each status is one of 'delivered', 'cancelled', or 'returned', and the order amount is always present (never NULL).
Calculate the metrics using conditional aggregation: a CASE expression inside an aggregate function.
Return these columns:
- store — store name (text);
- total_orders — total number of orders for the store (integer);
- delivered_cnt — number of orders with status 'delivered' (integer);
- cancelled_cnt — number of orders with status 'cancelled' (integer);
- delivered_revenue — sum of amount for delivered orders only, rounded to 2 decimal places; return 0 if there are no delivered orders.
Sort by store in ascending order. Return exactly one row for each store in the table. Orders with status 'returned' contribute only to total_orders.
storetotal_ordersdelivered_cntcancelled_cntdelivered_revenueSign in to see submission history
Sign inSign in to use AI Mentor
Sign in