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

Instruction

Let's get started! Suppose we want to know the average total price paid (before discount) per order.

We know that we can use SUM(unit_price * quantity) to calculate the total price for a single order. However, we now need the average total price for all orders. In other words, we would like to use the AVG() function on values calculated by a call to SUM(unit_price * quantity). Unfortunately, SQL doesn't allow this type of construction: AVG(SUM(unit_price * quantity)).

What can we do? Let's find out!

Exercise

Run the template query. It answers the following question: What is the average total price paid (before discounts) per order? Take a look at the answer, and don't worry about the code – we'll analyze it in a second.