Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Multiple metrics for a single object
Metrics for two groups
Ratios and percentages
9. Calculating percentages – step 4
Global vs. specific metrics
Summary

Instruction

Great work! We originally wanted to see a percentage, so let's modify our query a little bit to calculate a percentage instead of a ratio. Here's step 4:

SELECT
  COUNT(CASE
    WHEN shipped_date IS NOT NULL
      THEN order_id
  END) AS count_shipped,
  COUNT(order_id) AS count_all,
  ROUND(COUNT(CASE
    WHEN shipped_date IS NOT NULL
      THEN order_id
  END) / COUNT(order_id)::decimal * 100, 2) AS shipped_ratio
FROM orders;

Note that we multiply the result of the division inside the ROUND() function by 100 to get a percentage instead of a ratio.

Exercise

What is the percentage of discontinued items at Northwind? Show three columns: count_discontinued, count_all, and percentage_discontinued. Round the last column to two decimal places.

Stuck? Here's a hint!

Use the discontinued column from the products table. Remember to cast the denominator to decimal and to multiply the result by 100 to get a percentage.