Example 1
Count shipped orders and sum pending amounts per customer
Three separate metrics are computed in one GROUP BY pass. SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) counts only shipped rows. SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) totals amounts only for pending rows. Bob has two shipped orders and zero pending value; Carol has one pending order worth 75.
CREATE TABLE orders (
id INT,
customer VARCHAR(50),
status VARCHAR(20),
amount INT
);
INSERT INTO
orders (id, customer, status, amount)
VALUES
(1, 'Alice', 'shipped', 100),
(2, 'Alice', 'pending', 50),
(3, 'Bob', 'shipped', 200),
(4, 'Bob', 'shipped', 150),
(5, 'Carol', 'pending', 75);SELECT
customer,
COUNT(*) AS total_orders,
SUM(
CASE
WHEN status = 'shipped' THEN 1
ELSE 0
END
) AS shipped,
SUM(
CASE
WHEN status = 'pending' THEN amount
ELSE 0
END
) AS pending_value
FROM
orders
GROUP BY
customer
ORDER BY
customer;| customer | total_orders | shipped | pending_value |
|---|---|---|---|
| Alice | 2 | 1 | 50 |
| Bob | 2 | 2 | 0 |
| Carol | 1 | 0 | 75 |
Output is identical across all engines.