SQL Server CTE 递归查询全解

 在TSQL脚本中,也能实现递归查询,SQL Server提供CTE(Common Table Expression),只需要编写少量的代码,就能实现递归查询,本文详细介绍CTE递归调用的特性和使用示例,递归查询主要用于层次结构的查询,从叶级(Leaf Level)向顶层(Root Level)查询,或从顶层向叶级查询,或递归的路径(Path)。

一,递归查询原理

CTE的递归查询必须满足三个条件:初始条件,递归调用表达式,终止条件,CTE 递归查询的伪代码如下:

WITH cte_name ( column_name [,...n] ) AS
(
    --Anchor member is defined
    CTE_query_definition 
    UNION ALL
    --Recursive member is defined referencing cte_name
    CTE_query_definition 
)
-- Statement using the CTE
SELECT * FROM cte_name

1,递归查询至少包含两个子查询:

  • 第一个子查询称作定点(Anchor)子查询:定点查询只是一个返回有效表的查询,用于设置递归的初始值;
  • 第二个子查询称作递归子查询:该子查询调用CTE名称,触发递归查询,实际上是递归子查询调用递归子查询
  • 两个子查询使用union all,求并集;

2,CTE的递归终止条件

递归查询没有显式的递归终止条件,只有当递归子查询返回空结果集(没有数据行返回)或是超出了递归次数的最大限制时,才停止递归。

默认的递归查询次数是100,可以使用查询提示(hint):MAXRECURSION 控制递归的最大次数:OPTION( MAXRECURSION 16);如果允许无限制的递归次数,使用查询提示:option(maxrecursion 0);当递归查询达到指定或默认的 MAXRECURSION 数量限制时,SQL Server将结束查询并返回错误,如下:

The statement terminated. The maximum recursion 10 has been exhausted before statement completion.

事务执行失败,该事务包含的所有操作都被回滚。在产品环境中,慎用maxrecursion 查询提示,推荐通过 where 条件限制递归的次数。

3,递归步骤

step1:定点子查询设置CTE的初始值,即CTE的初始值Set0;

递归调用的子查询过程:递归子查询调用递归子查询;

step2:递归子查询第一次调用CTE名称,CTE名称是指CTE的初始值Set0,第一次执行递归子查询之后,CTE名称是指结果集Set1;

step3:递归子查询第二次调用CTE名称,CTE名称是指Set1,第二次执行递归子查询之后,CTE名称是指结果集Set2;

step4:在第N次执行递归子查询时,CTE名称是指Set(N-1),递归子查询都引用前一个递归子查询的结果集;

Step5:如果递归子查询返回空数据行,或超出递归次数的最大限制,停止递归;


 二,递归查询示例(员工职称)

1,创建测试数据

ManagerID是UserID的父节点,这是一个非常简单的层次结构模型。

CREATE TABLE dbo.dt_user (UserID INT, ManagerID INT, Name NVARCHAR(10));
INSERT INTO dbo.dt_user
SELECT 1, -1, N'Boss' UNION ALL
SELECT 11, 1, N'A1' UNION ALL 
SELECT 12, 1, N'A2' UNION ALL
SELECT 13, 1, N'A3' UNION ALL
SELECT 111, 11, N'B1' UNION ALL
SELECT 112, 11, N'B2' UNION ALL
SELECT 121, 12, N'C1';

2,查询每个User的的直接上级Manager

;WITH cte AS
    (SELECT UserID, ManagerID, name, name AS ManagerName
     FROM   dbo.dt_user
     WHERE  ManagerID=-1
     UNION ALL
     SELECT c.UserID, c.ManagerID, c.Name, P.name AS ManagerName
     FROM   cte P
            INNER JOIN dbo.dt_user c ON P.UserID=c.ManagerID)
SELECT UserID, ManagerID, Name, ManagerName FROM cte ORDER BY UserID;

step1:查询ManagerID=-1,作为root node,这是递归查询的起始点。

step2:迭代公式是 union all 下面的查询语句。在查询语句中调用中cte,而查询语句就是cte的组成部分,即 “自己调用自己”,这就是递归的真谛所在。

所谓迭代,是指每一次递归都要调用上一次查询的结果集,Union ALL是指每次都把结果集并在一起。

step3-N,迭代公式利用上一次查询返回的结果集执行特定的查询,直到CTE返回null 或达到最大的迭代次数,默认值是32。最终的结果集是迭代公式返回的各个结果集的并集,求并集是由Union All 子句定义的,并且只能使用Union ALL。

SQL Server CTE 递归查询全解_第1张图片

3,查询路径,在层次结构中查询子节点到父节点的path

;WITH cte AS
    (SELECT UserID, ManagerID, name, CAST(name AS NVARCHAR(MAX)) AS ReportPath
     FROM   dbo.dt_user
     WHERE  ManagerID=-1
     UNION ALL
     SELECT c.UserID, c.ManagerID, c.Name, c.name+'->'+P.ReportPath AS ReportPath
     FROM   cte P
            INNER JOIN dbo.dt_user c ON P.UserID=c.ManagerID)
SELECT UserID, ManagerID, Name, ReportPath FROM cte ORDER BY UserID;

查询结果如下截图:

SQL Server CTE 递归查询全解_第2张图片


三,递归查询示例(行政区划)

1,需求模拟

在TSQL中实现层次结构,例如有这样一种数据结构,省,市,县,乡,村,如何使用一张表表示这种数据结构,并且允许是不对称的,例如,上海市是个直辖市,没有省份。

CREATE TABLE dbo.hierarchy (ID INT NOT NULL PRIMARY KEY,
                            --Type int not null,
                            ParentID INT NOT NULL,
                            name VARCHAR(100) NOT NULL);

type表示类型,可以设置:省,Type是1;市,type是2,以此类推。

ParentID标识的是父级ID,例如信阳市的ParentID是河南省的ID。

2,插入测试数据

测试数据格式说明了归属关系,博主懒,去掉type字段。

INSERT INTO dbo.hierarchy
VALUES(1, 0, '河南省'),(2, 1, '信阳市'),(3, 2, '淮滨县'),(4, 3, '芦集乡'),(12, 3, '邓湾乡'),(13, 3, '台头乡'),
(14, 3, '谷堆乡'),(8, 2, '固始县'),(9, 8, '李店乡'),(10, 2, '息县'),(11, 10, '关店乡'),(5, 1, '安阳市'),
(6, 5, '滑县'),(7, 6, '老庙乡'),(15, 1, '南阳市'),(16, 15, '方城县'),(17, 1, '驻马店市'),(18, 17, '正阳县');
SELECT * FROM dbo.hierarchy ORDER BY ParentID;

3,实现由父级向子级的查询

由于实际的数据可能有很多,所以,要想获取河南省下的所有市,县,乡,村等信息,必须使用递归查询

;WITH cte(Id, ParentID, Name) AS
    (SELECT * FROM dbo.hierarchy WHERE id=1
     UNION ALL
     SELECT h.* FROM dbo.hierarchy h INNER JOIN cte c ON h.ParentID=c.id
--where c.id!=h.ID
)
SELECT * FROM cte ORDER BY ParentID;

如果要查看向内递归到多少level,可以使用派生列,level=0是省level,level=1是市level,依次类推。

;WITH cte(Id, ParentID, Name, Level) AS
    (SELECT ID, ParentID, Name, 0 AS Level FROM dbo.hierarchy WHERE id=1
     UNION ALL
     SELECT h.ID, h.ParentID, h.Name, c.Level+1 AS Level
     FROM   dbo.hierarchy h
            INNER JOIN cte c ON h.ParentID=c.id
--where c.id!=h.ID
)
SELECT * FROM cte ORDER BY ParentID;

查询结果如图:

SQL Server CTE 递归查询全解_第3张图片

4,由子级向父级的递归查询

;WITH cte AS
    (SELECT ID, ParentID, name FROM dbo.hierarchy WHERE id=4 --芦集乡的ID
     UNION ALL
     SELECT h.ID, h.ParentID, h.name
     FROM   dbo.hierarchy h
            INNER JOIN cte c ON h.id=c.ParentID)
SELECT ID, ParentID, name FROM cte ORDER BY ParentID;

查询结果如图:

SQL Server CTE 递归查询全解_第4张图片

 


四,递归查询示例(账单流水) 

SQL Server CTE 递归查询全解_第5张图片

 

DECLARE @tb TABLE (Debtor REAL,Creditor REAL,Direction NVARCHAR(1),Remainder REAL)
INSERT INTO @tb(Direction,Remainder) VALUES ('',84.9000)
INSERT INTO @tb(Debtor,Creditor) VALUES (3000.000,0.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,800.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,2284.9000)
INSERT INTO @tb(Debtor,Creditor) VALUES (1144.0000,0.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,1144.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (5000.0000,0.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,5000.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (436.0000,0.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,436.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,4000.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (5000.0000,0.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,960.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,800.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (800.0000,0.0000)
INSERT INTO @tb(Debtor,Creditor) VALUES (0.0000,40.0000)

;WITH TempDCR AS(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS 'ID' ,Debtor,Creditor,Direction,Remainder FROM @tb)
, TempReCursion AS(
SELECT TOP 1 ID, Debtor,Creditor,Remainder,Direction FROM TempDCR
UNION ALL
SELECT a.ID,a.Debtor,a.Creditor,b.Remainder+a.Debtor-a.Creditor,Direction=CASE WHEN a.Debtor>0 THEN N'' ELSE N'' END 
    FROM TempDCR a JOIN TempReCursion b ON a.ID=b.ID+1
)
SELECT Debtor,Creditor,Direction,Remainder FROM TempReCursion

 


  五,递归查询示例(层级汇总) 

SQL Server CTE 递归查询全解_第6张图片

--测试数据
with area(id,"name",f_id,leve) as (
  select  1,'中国',0,1 union all
  select  2,'湖北',1,2 union all
  select  3,'武汉',2,3 union all
  select  4,'云贵',1,2 union all
  select  5,'云南',4,3 union all
  select  6,'贵阳',4,3 union all
  select  7,'云南子区',5,4 union all
  select  8,'贵阳子区',6,4 union all
  select  9,'蔡甸',2,3
), "table"(id,area_id,"money") as (
  select  1,3,10 union all
  select  2,9,5 union all
  select  3,7,20 union all
  select  4,8,30
)
--使用cte递归求出每个节点的路径
,t(id,f_id,"name","level",fullpath) as (
  select a.id,a.f_id,a."name",a.leve,cast(a.id as varchar(max))
  from area a
  where a.leve=1
  union all
  select b.id,b.f_id,b."name",b.leve,t.fullpath+'->'+cast(b.id as varchar(max))
  from area b
  inner join t on t.id=b.f_id
)
--汇总统计每个节点的金额
select t.id,t."name",t."level",sum(c."money") as "money"
from t
inner join t t1 on charindex(t.fullpath,t1.fullpath)=1
inner join "table" c on c.area_id=t1.id
group by t.id,t."name",t."level"
--having t."level"=2 --筛选出第二层级
--order by t.id
--另一个例子
WITH tb(child,parent,[money]) AS (
    SELECT '水果',NULL,1 UNION ALL
    SELECT '苹果','水果',2 UNION ALL
    SELECT '桃子','水果',3 UNION ALL
    SELECT '黄桃','桃子',4 UNION ALL
    SELECT '富士','苹果',5 UNION ALL
    SELECT '红富士','富士',6)
,CTE AS( 
    SELECT child,parent,money,[path]=CAST(child AS VARCHAR(100)) FROM tb WHERE parent IS NULL UNION ALL
    SELECT a.child,a.parent,a.money,CAST(b.path+'->'+a.child AS VARCHAR(100)) FROM tb a JOIN CTE b ON a.parent=b.child
)
SELECT a.child,SUM(b.money) total 
FROM CTE a JOIN CTE b ON CHARINDEX(a.path,b.path)=1 
GROUP BY a.child
ORDER BY total

 

转载于:https://www.cnblogs.com/zhaoshujie/p/9594720.html

你可能感兴趣的:(SQL Server CTE 递归查询全解)