Back to problems

Daily Metrics by Order Status in a Single SQL Pass

SQL · Tesla · Medium

You are given an orders table containing order_id, status (pending, completed, or cancelled), amount, and date. For each date, calculate these values: total_revenue: the sum of amount from completed orders only. pending_count: the number of pending orders. cancelled_count: the number of cancelled orders. Requirements: Write a single SQL query. Use conditional aggregation so the data is processed in one pass when possible. Return one result group for each date. Example

Checking your access…