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 ship_country and avg_distinct_item_count 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 ship_country column. In the outer query, apply the proper aggregation.

Stuck? Here's a hint!

Use the following template:

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