1-- Define the CTE expression name and column list.
2WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)
3AS
4-- Define the CTE query.
5(
6 SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
7 FROM Sales.SalesOrderHeader
8 WHERE SalesPersonID IS NOT NULL
9)
10-- Define the outer query referencing the CTE name.
11SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
12FROM Sales_CTE
13GROUP BY SalesYear, SalesPersonID
14ORDER BY SalesPersonID, SalesYear;
15