with ... as
作用:指定临时命名的结果集,类似于临时表。
但是,这个结果集被称作:公用表表达式(CTE) - common_table_expression
可以在select , insert , update , delete , merge语句的执行范围定义。
公用表表达式可以包括对自身的引用。所以,这种公用表表达式也称为递归公用表表达式。
语法:
[ WITH <common_table_expression> [ ,...n ] ] <common_table_expression>::= expression_name [ ( column_name [ ,...n ] ) ] AS ( CTE_query_definition )
expression_name
是公用表表达式的有效标识符。即名称
column_name
在公用表表达式中指定列名。
当在查询定义中为所有结果列都提供了不同的名称时,列名是可选的。即此时可以不写。
CTE_query_definition
指定一个其结果集填充公用表表达式的 SELECT 语句。即查询定义。
如果定义了多个 CTE_query_definition,则这些查询定义必须用下列一个集合运算符联接起来 :UNION ALL、UNION、EXCEPT 或 INTERSECT。
示例1
显示每名销售代表每年的销售订单总数。
1.数据准备
create table SalesOrderHeader ( SalesPersonID varchar(50), SalesOrderID varchar(50), OrderDate varchar(50) ) --drop table SalesOrderHeader insert into SalesOrderHeader values ('01','20200330001','20200330') ,('02','20200330002','20200330') ,('02','20200331001','20200331')
2.使用
-- Define the CTE expression name and column list. WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear) AS -- Define the CTE query. ( SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear FROM SalesOrderHeader WHERE SalesPersonID IS NOT NULL ) -- Define the outer query referencing the CTE name. SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear FROM Sales_CTE GROUP BY SalesYear, SalesPersonID ORDER BY SalesPersonID, SalesYear;
或
省略列名(在查询中定义了列名)
-- Define the CTE expression name and column list. WITH Sales_CTE AS -- Define the CTE query. ( SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear FROM SalesOrderHeader WHERE SalesPersonID IS NOT NULL ) -- Define the outer query referencing the CTE name. SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear FROM Sales_CTE GROUP BY SalesYear, SalesPersonID ORDER BY SalesPersonID, SalesYear;
3.效果
示例2
如何在单个查询中定义多个CTE
WITH Sales_CTE (SalesPersonID, TotalSales, SalesYear) AS -- Define the first CTE query. ( SELECT SalesPersonID, SUM(TotalDue) AS TotalSales, YEAR(OrderDate) AS SalesYear FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL GROUP BY SalesPersonID, YEAR(OrderDate) ) , -- Use a comma to separate multiple CTE definitions. -- Define the second CTE query, which returns sales quota data by year for each sales person. Sales_Quota_CTE (BusinessEntityID, SalesQuota, SalesQuotaYear) AS ( SELECT BusinessEntityID, SUM(SalesQuota)AS SalesQuota, YEAR(QuotaDate) AS SalesQuotaYear FROM Sales.SalesPersonQuotaHistory GROUP BY BusinessEntityID, YEAR(QuotaDate) ) -- Define the outer query by referencing columns from both CTEs. SELECT SalesPersonID , SalesYear , FORMAT(TotalSales,'C','en-us') AS TotalSales , SalesQuotaYear , FORMAT (SalesQuota,'C','en-us') AS SalesQuota , FORMAT (TotalSales -SalesQuota, 'C','en-us') AS Amt_Above_or_Below_Quota FROM Sales_CTE JOIN Sales_Quota_CTE ON Sales_Quota_CTE.BusinessEntityID = Sales_CTE.SalesPersonID AND Sales_CTE.SalesYear = Sales_Quota_CTE.SalesQuotaYear ORDER BY SalesPersonID, SalesYear;
示例1和示例2是非递归方式,示例3演示递归方式
示例3
示例3显示经理以及向经理报告的雇员的层次列表。
1.数据准备
-- Create an Employee table. CREATE TABLE dbo.MyEmployees ( EmployeeID smallint NOT NULL, FirstName nvarchar(30) NOT NULL, LastName nvarchar(40) NOT NULL, Title nvarchar(50) NOT NULL, DeptID smallint NOT NULL, ManagerID int NULL, CONSTRAINT PK_EmployeeID PRIMARY KEY CLUSTERED (EmployeeID ASC) ); -- Populate the table with values. INSERT INTO dbo.MyEmployees VALUES (1, N'Ken', N'Sánchez', N'Chief Executive Officer',16,NULL) ,(273, N'Brian', N'Welcker', N'Vice President of Sales',3,1) ,(274, N'Stephen', N'Jiang', N'North American Sales Manager',3,273) ,(275, N'Michael', N'Blythe', N'Sales Representative',3,274) ,(276, N'Linda', N'Mitchell', N'Sales Representative',3,274) ,(285, N'Syed', N'Abbas', N'Pacific Sales Manager',3,273) ,(286, N'Lynn', N'Tsoflias', N'Sales Representative',3,285) ,(16, N'David',N'Bradley', N'Marketing Manager', 4, 273) ,(23, N'Mary', N'Gibson', N'Marketing Specialist', 4, 16);
2.递归使用演示
WITH DirectReports(ManagerID, EmployeeID, Title, EmployeeLevel) AS ( SELECT ManagerID, EmployeeID, Title, 0 AS EmployeeLevel FROM dbo.MyEmployees WHERE ManagerID IS NULL UNION ALL SELECT e.ManagerID, e.EmployeeID, e.Title, EmployeeLevel + 1 FROM dbo.MyEmployees AS e INNER JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID ) SELECT ManagerID, EmployeeID, Title, EmployeeLevel FROM DirectReports ORDER BY ManagerID;
3.效果
示例4
下例演示在单个语句中包含两个 CTE。 CTE 不能嵌套(无递归)
WITH CountDate (TotalCount, TableName) AS ( SELECT COUNT(datekey), 'DimDate' FROM DimDate ) , CountCustomer (TotalAvg, TableName) AS ( SELECT COUNT(CustomerKey), 'DimCustomer' FROM DimCustomer ) SELECT TableName, TotalCount FROM CountDate UNION ALL SELECT TableName, TotalAvg FROM CountCustomer;