Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Basic multi-level aggregation
Multi-level aggregation in groups
9. Multi-level aggregation in groups – exercise 1
Multi-level aggregation with custom classification
Three or more aggregation levels
Summary

Instruction

Okay! Let's do two similar exercises to practice the pattern.

Exercise

For each shipping country, we want to find the average count of unique products in each order. Show the ShipCountry and AvgDistinctItemCount columns. Sort the results by count, in descending order.

In the inner query, find the number of distinct products in each order and select it alongside the ShipCountry column. In the outer query, apply the proper aggregation.

Stuck? Here's a hint!

Use the following template:

WITH OrderDistinctItems AS (
  ...
)
SELECT ...
FROM OrderDistinctItems
GROUP BY ...
ORDER BY ... DESC;