Excellent! How can we compute percentages and ratios in our reports? Let's find out.
Suppose we want to know what percentage of all orders have already been shipped. We'll write this query in a few steps. Here's step 1:
WHEN shipped_date IS NOT NULL
END) AS count_shipped,
COUNT(order_id) AS count_all
In this step, we simply calculate two separate columns: the numerator and the denominator of our ratio. In this case, the numerator is the number of orders shipped and the denominator is the total number of orders.
Note how we used
CASE WHEN to compute one metric (number of orders shipped) and
COUNT() to compute the other metric (total number of orders).