Brightlane's reporting pipeline materializes an order-status summary into a temp table to avoid rerunning the aggregation for every downstream report. The query that populates the temp table needs to return the order count and combined order amount for each status value.
Write a query to return the status, order count, and total amount for each status value.
Assumptions:
- The
orderstable has one row per order with astatusand atotal_amount. - Each unique
statusvalue should appear once in the result. - For each status, the order count is the number of orders carrying that
status. The total amount is the combinedtotal_amountacross those orders.
Output:
- One row per status, with columns
status,order_count, andstatus_total.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
status,
COUNT(*) AS order_count,
SUM(total_amount) AS status_total
FROM
orders
GROUP BY
status The shape
A GROUP BY status collapses the orders table to one row per distinct status, and the aggregates produce the count and combined amount for each. This is the exact shape a temp table populated by CREATE TEMP TABLE ... AS SELECT would carry: one row per category, with the metrics already rolled up.
Clause by clause
SELECT status, COUNT(*) AS order_count, SUM(total_amount) AS status_totalreturns three columns. Thestatusvalue labels each group;COUNT(*)counts the orders inside that group (sodeliveredreports 161);SUM(total_amount)adds up every order amount in the group.FROM ordersreads every order in the table. Every row participates because noWHEREfilter is applied.GROUP BY statusis what makes the aggregates per-status rather than per-table. Without it,COUNT(*)andSUM(total_amount)would collapse the entire table into a single row of two grand totals. With it, the engine sorts rows into one bucket per distinctstatusvalue and runs the aggregates inside each bucket separately.
Why this and not the per-row table itself
The pipeline materializes this rolled-up shape, not the raw orders, because the downstream reports want totals by status, not individual order rows. Materializing the aggregated result once means each downstream query reads four rows instead of re-scanning every order and re-running the aggregation. The query you wrote is the canonical body of a CTAS used for exactly that caching purpose.
You practiced producing a per-category aggregation suitable for materialization — the same SELECT shape can either feed a one-shot result set or populate a CREATE TEMP TABLE ... AS SELECT.