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 ShippedDate IS NOT NULL
END) AS CountShipped,
COUNT(OrderID) AS CountAll
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).