On January 21th at 14:15 UTC , progression through exercises will be unavailable for 10 minutes due to a planned maintenance break.
Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Reports with revenue in categories


Good job! Now that we know how to add TotalRevenue as the last column, we can calculate the ratio between a category's revenue and total revenue. Such a report can help us quickly identify which categories generate the most revenue. Here's the query:

WITH AllCategories AS (
  SELECT SUM(Amount) AS TotalSum
  FROM Orders
  SUM(OI.Amount) AS CategoryRevenue,
  SUM(OI.Amount) / CAST(AC.TotalSum AS decimal(10,2)) AS TotalRevenueRatio
FROM AllCategories AC, OrderItems OI
JOIN Orders O
  ON O.OrderId = OI.OrderId
JOIN Products P
  ON OI.ProductId = P.ProductId
JOIN Categories C
  ON P.CategoryId = C.CategoryId
GROUP BY CategoryName, AC.TotalSum;

In the third column, we now divide category revenue by total revenue and get the correct ratio. As usual, we're casting one of the values to a decimal type to avoid integer division.


Create a report of the category revenue for all orders placed in 2017 or later. Show three columns: CategoryName, CategoryRevenue, and TotalRevenuePercentage. The last column should show the rounded to two decimal places percentage of the total revenue generated by that category.

Stuck? Here's a hint!

Filter rows in both the CTE and the outer query. To get percentages, multiply the ratio of category revenue to total revenue by 100.