Simple CTE Example
WITH cteTotalSales (SalesPersonID, NetSales) AS ( SELECT SalesPersonID, ROUND(SUM(SubTotal), 2) FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL GROUP BY SalesPersonID ) SELECT sp.FirstName + ' ' + sp.LastName AS FullName, sp.City + ', ' + StateProvinceName AS Location, ts.NetSales FROM Sales.vSalesPerson AS sp INNER JOIN cteTotalSales AS ts ON sp.BusinessEntityID = ts.SalesPersonID ORDER BY ts.NetSales DESC
Concatenated CTE Example
WITH cteTotalSales (SalesPersonID, NetSales) AS ( SELECT SalesPersonID, ROUND(SUM(SubTotal), 2) FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL AND OrderDate BETWEEN '2003-01-01 00:00:00.000' AND '2003-12-31 23:59:59.000' GROUP BY SalesPersonID ), cteTargetDiff (SalesPersonID, SalesQuota, QuotaDiff) AS ( SELECT ts.SalesPersonID, CASE WHEN sp.SalesQuota IS NULL THEN 0 ELSE sp.SalesQuota END, CASE WHEN sp.SalesQuota IS NULL THEN ts.NetSales ELSE ts.NetSales - sp.SalesQuota END FROM cteTotalSales AS ts INNER JOIN Sales.SalesPerson AS sp ON ts.SalesPersonID = sp.BusinessEntityID ) SELECT sp.FirstName + ' ' + sp.LastName AS FullName, sp.City, ts.NetSales, td.SalesQuota, td.QuotaDiff FROM Sales.vSalesPerson AS sp INNER JOIN cteTotalSales AS ts ON sp.BusinessEntityID = ts.SalesPersonID INNER JOIN cteTargetDiff AS td ON sp.BusinessEntityID = td.SalesPersonID ORDER BY ts.NetSales DESC
Recursive CTE
--To demonstrate how the recursive CTE works, I used the following --Transact-SQL statements to create and populate the Employees table IF OBJECT_ID('Employees', 'U') IS NOT NULL DROP TABLE dbo.Employees GO CREATE TABLE dbo.Employees ( EmployeeID int NOT NULL PRIMARY KEY, FirstName varchar(50) NOT NULL, LastName varchar(50) NOT NULL, ManagerID int NULL ) GO INSERT INTO Employees VALUES (101, 'Ken', 'Sánchez', NULL) INSERT INTO Employees VALUES (102, 'Terri', 'Duffy', 101) INSERT INTO Employees VALUES (103, 'Roberto', 'Tamburello', 101) INSERT INTO Employees VALUES (104, 'Rob', 'Walters', 102) INSERT INTO Employees VALUES (105, 'Gail', 'Erickson', 102) INSERT INTO Employees VALUES (106, 'Jossef', 'Goldberg', 103) INSERT INTO Employees VALUES (107, 'Dylan', 'Miller', 103) INSERT INTO Employees VALUES (108, 'Diane', 'Margheim', 105) INSERT INTO Employees VALUES (109, 'Gigi', 'Matthew', 105) INSERT INTO Employees VALUES (110, 'Michael', 'Raheem', 106) --Recursive CTE WITH cteReports (EmpID, FirstName, LastName, MgrID, EmpLevel) AS ( SELECT EmployeeID, FirstName, LastName, ManagerID, 1 FROM Employees WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID, e.FirstName, e.LastName, e.ManagerID, r.EmpLevel + 1 FROM Employees e INNER JOIN cteReports r ON e.ManagerID = r.EmpID ) SELECT FirstName + ' ' + LastName AS FullName, EmpLevel, (SELECT FirstName + ' ' + LastName FROM Employees WHERE EmployeeID = cteReports.MgrID) AS Manager FROM cteReports ORDER BY EmpLevel, MgrID