Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement. This clause can also be used in a CREATE VIEW statement as part of its defining SELECT statement.
-- Define the CTE expression name and column list.
WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)
-- Define the CTE query.
SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
WHERE SalesPersonID IS NOT NULL
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;