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
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 ShippedDate IS NOT NULL
      THEN OrderID
  END) AS CountShipped,
  COUNT(OrderID) AS CountAll,
  ROUND(COUNT(CASE
    WHEN ShippedDate IS NOT NULL
      THEN OrderID
  END) / CAST(COUNT(OrderID) AS float) * 100, 2) AS ShippedRatio
FROM Orders;

Note that we multiply the result of the 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: CountDiscontinued, CountAll, and PercentageDiscontinued. 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 float and to multiply the result by 100 to get a percentage.