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

Instruction

Good job! There is also an alternative CTE syntax that looks like this:

WITH OrderTotalPrices (OrderID, TotalPrice) AS (
  SELECT
    O.OrderID, 
    SUM(UnitPrice * Quantity)
  FROM Orders O
  JOIN OrderItems OI
    ON O.OrderID = OI.OrderID
  GROUP BY O.OrderID
)

SELECT
  AVG(TotalPrice) AS AvgTotalPrice
FROM OrderTotalPrices;

This time, we provided the column names right after the CTE's name, inside a pair of parentheses (WITH OrderTotalPrices (OrderID, TotalPrice) AS ...). This way, we didn't have to use the AS keyword to name the columns inside the CTE.

This syntax is completely optional. It's up to you whether you want to use it and it depends on your personal preferences.

Exercise

Try to rewrite the template query so that it uses the syntax presented in the explanation.

Stuck? Here's a hint!

Start with:

WITH ProductsCategoryCount (CategoryID, CountProducts) AS (