• SQL SERVER CTE递归查询


    SQL SERVER CTE递归查询

    正文

      SQL SERVER 2005之前的版本只能用函数方法实现,SQL SERVER 2005之后新增了CTE(Common Table Expression)功能,可以利用CTE实现递归查询;

     

    一、建表

    1、sql

     

    Create table test_GroupInfo([Id] int,[GroupName] nvarchar(50),[ParentGroupId] int)
    
    Insert test_GroupInfo
    
    select 0,'某某大学',null union all
    
    select 1,'外语学院',0 union all
    select 2,'英语专业',1 union all
    select 3,'日语专业',1 union all
    select 4,'英语专业一班',2 union all
    select 5,'英语专业二班',2 union all
    select 6,'日语专业一班',3 union all
    select 7,'日语专业二班',3 union all
    
    select 8, '法学院',0 union all
    select 9, '刑法学专业',8 union all
    select 10,'经济法学专业',8 union all
    select 11,'刑法学专业一班',9 union all
    select 12,'刑法学专业二班',9 union all
    select 13,'经济法学专业一班',10 union all
    select 14,'经济法学专业二班',10

    2、效果图

     

     

    二、递归实现Demo

    1、根据指定的节点向上获取所有父节点,向下获取所有子节点

    --根据指定的节点向下获取所有子节点
    with
    CTE
    as
    (
        select * from test_GroupInfo where Id=1
        union all
        select G.* from CTE inner join test_GroupInfo as G
        on CTE.Id=G.ParentGroupId
    )
    select * from CTE order by Id

    --根据指定的节点向上获取所有父节点
    with
    CTE
    as
    (
        select * from test_GroupInfo where Id=14
        union all
        select G.* from CTE inner join test_GroupInfo as G
        on CTE.ParentGroupId=G.Id
    )
    select * from CTE order by Id

     2、构造递归路径

    --构造递归路径
    with
    CTE
    as
    (
        select Id,GroupName,ParentGroupId,GroupPath=CAST( GroupName as nvarchar(max)) from test_GroupInfo where Id=1
        union all
        select G.*,CAST(CTE.GroupPath+'//'+G.GroupName as nvarchar(max)) as GroupPath from CTE
        inner join test_GroupInfo as G
        on CTE.Id=G.ParentGroupId
    )
    select * from CTE

     3、分组递归,将同一条分支上节点放到一起

    --通过id字段的字符串的拼接,形成sort字段,再通过sort排序,来实现同一分支上的节点放到一起
    WITH
    CTE
    AS
    (
        SELECT * ,CAST(RIGHT('000' + CAST([Id] AS VARCHAR), 3) AS VARCHAR(MAX)) AS sort FROM test_GroupInfo
        WHERE ParentGroupId = 0
        UNION ALL
        SELECT   test_GroupInfo.* ,CAST(sort + RIGHT('000' + CAST(test_GroupInfo.[Id] AS VARCHAR),3) AS VARCHAR(MAX)) AS sort
        FROM CTE
        INNER JOIN test_GroupInfo ON CTE.Id = test_GroupInfo.ParentGroupId
    )
    SELECT * FROM CTE ORDER BY sort
    
    
    select id,CAST(RIGHT('000' + CAST([Id] AS VARCHAR), 3) AS VARCHAR(MAX)) from test_GroupInfo

    4、递归层级查询(查询出节点所属的层级)

    --查询节点层级
    WITH CTE AS (
        SELECT *,1 AS [Level] FROM test_GroupInfo WHERE ParentGroupId=0
        UNION ALL
        SELECT G.*,CTE.Level+1 FROM test_GroupInfo as G
        JOIN CTE ON CTE.Id =G.ParentGroupId
    )
    SELECT * FROM CTE

    转载自:https://www.cnblogs.com/linyuansun/p/14283550.html

  • 相关阅读:
    android学习十四(android的接收短信)
    C/C++知识要点4——printf函数以及cout的计算顺序
    HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)
    微信错误提示code= -4/微信发送被拒绝
    struts2的validate在使用过程中的一个问题
    28.字符串的排列
    Redis入门经典——The Little Redis Book (翻译)
    POJ 3155 Hard Life(最大密度子图)
    BZOJ 1798 AHOI2009 Seq 维护序列 线段树
    RT-Thread开篇
  • 原文地址:https://www.cnblogs.com/guohu/p/14838407.html
Copyright © 2020-2023  润新知