五维思考

学习要加,骄傲要减,机会要乘,懒惰要除。 http://www.5dthink.cn

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

一、递归查询原理

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

WITH cte_name ( column_name [,...n] ) AS (
    CTE_query_definition
	UNION ALL
    CTE_query_definition
)
SELECT * FROM cte_name

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

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

2、CTE的递归终止条件

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

默认的递归查询次数是100,可以使用查询提示: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、递归步骤

  1. 定点子查询设置CTE的初始值,即CTE的初始值Set0;
  2. 递归子查询第一次调用CTE名称,CTE名称是指CTE的初始值Set0,第一次执行递归子查询之后,CTE名称是指结果集Set1
  3. 递归子查询第二次调用CTE名称,CTE名称是指Set1,第二次执行递归子查询之后,CTE名称是指结果集Set2
  4. 在第N次执行递归子查询时,CTE名称是指Set(N-1),递归子查询都引用前一个递归子查询的结果集;
  5. 如果递归子查询返回空数据行,或超出递归次数的最大限制,停止递归;

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

1、创建测试数据

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

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;
  1. 查询ManagerID=-1,作为root node,这是递归查询的起始点。
  2. 迭代公式是union all下面的查询语句。在查询语句中调用中cte,而查询语句就是cte的组成部分,即"自己调用自己",这就是递归的真谛所在。所谓"迭代",是指每一次递归都要调用上一次查询的结果集,Union ALL是指每次都把结果集并在一起。
  3. 迭代公式利用上一次查询返回的结果集执行特定的查询,直到cte返回NULL或达到最大的迭代次数,默认值是32。最终的结果集是迭代公式返回的各个结果集的并集,求并集是由UNION ALL子句定义的,并且只能使用UNION ALL

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;

查询结果如下截图:


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

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=1,依次类推。

;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;

查询结果如图:

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;

查询结果如图:

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

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

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

--测试数据
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
posted on 2017-02-04 20:09  五维思考  阅读(1667)  评论(0编辑  收藏  举报

QQ群:1. 全栈码农【346906288】2. VBA/VSTO【2660245】