zoukankan      html  css  js  c++  java
  • sql server: Parent/Child hierarchy tree view

    sql server - Parent/Child hierarchy tree view - Stack Overflow

    ---
    declare @pc table(CHILD_ID int, PARENT_ID int, [NAME] varchar(80));
     
    insert into @pc
    select 1,NULL,'Bill' union all
    select 2,1,'Jane' union all
    select 3,1,'Steve' union all
    select 4,2,'Ben' union all
    select 5,3,'Andrew' union all
    select 6,NULL,'Tom' union all
    select 7,8,'Dick' union all
    select 8,6,'Harry' union all
    select 9,3,'Stu' union all
    select 10,7,'Joe';
     
     
    ; with r as (
          select CHILD_ID, PARENT_ID, [NAME], depth=0, sort=cast(CHILD_ID as varchar(max))
          from @pc
          where PARENT_ID is null
          union all
          select pc.CHILD_ID, pc.PARENT_ID, pc.[NAME], depth=r.depth+1, sort=r.sort+cast(pc.CHILD_ID as varchar(30))
          from r
          inner join @pc pc on r.CHILD_ID=pc.PARENT_ID
          where r.depth<32767
    )
    select tree=replicate('-',r.depth*3)+r.[NAME]
    from r
    order by sort
    option(maxrecursion 32767);
    GO
    
    
    
    --https://www.sqlteam.com/articles/more-trees-hierarchies-in-sql
    
    
    SELECT a.ID, a.CategoryName, COALESCE(b.CategoryName,'-') AS 'ParentName'
    FROM Category AS a LEFT JOIN Category AS b on a.ParentID = b.ID
    GO
    
    
    create table pc ( parent varchar(10), child varchar(10) )
    
    insert into pc values('a','b');
    insert into pc values('a','c');
    insert into pc values('b','e');
    insert into pc values('b','f');
    insert into pc values('a','d');
    Insert into pc values('b','g');
    insert into pc values('c','h');
    insert into pc values('c','i');
    insert into pc values('d','j');
    insert into pc values('f','k');
    insert into pc values('x','y');
    insert into pc values('y','z');
    insert into pc values('m','n');
    
     DECLARE @parent varchar(10) = 'a';
     WITH cte AS
     (
      select null parent, @parent child, 0 as level
       union
      SELECT  a.parent, a.child , 1 as level
        FROM pc a
       WHERE a.parent = @parent
       UNION ALL
      SELECT a.parent, a.child , c.level +    1
      FROM pc a JOIN cte c ON a.parent = c.child
      )
      SELECT distinct parent, child , level
      FROM cte
      order by level, parent
      go
    
      -- https://stackoverflow.com/questions/9676637/sql-parent-child-tree-with-sort-order
      -- https://stackoverflow.com/questions/49903446/sql-parent-child-tree-data-return-only-completed-tree-nodes-from-list
      -- https://www.sqlteam.com/articles/more-trees-hierarchies-in-sql
      declare @relations table(ParentID int, ChildID int, SortOrder int, treeID int);
    
    insert into @relations values
    (0,1,0,0), (1,2,1,0), (2,3,2,0), (2,4,1,0), (2,6,3,0), (1,7,2,0), (1,9,3,0), (9,10,1,0), (9,12,2,0) --tree 0
    , (0,1,0,1), (1,2,1,1), (2,3,2,1), (2,4,1,1), (2,6,3,1), (1,7,2,1), (1,9,3,1), (9,10,1,1), (9,12,2,1) --tree 1
    
    ; with cte(ParentId,ChildId,SortOrder,depth,agg,treeID) as (
        select null,ParentId,SortOrder,0
        , right('0000000'+CAST(treeID as varchar(max)),7)
            +right('0000000'+CAST(SortOrder as varchar(max)),7)
        , treeID
        from @relations where ParentId=0
        union all
        select cte.ChildId,r.ChildId,r.SortOrder,cte.depth+1
        , cte.agg
            +right('0000000'+CAST(r.treeID as varchar(max)),7)
            +right('0000000'+CAST(r.SortOrder as varchar(max)),7)
            +right('0000000'+CAST(r.ChildId as varchar(max)),7)
        , r.treeID
        from cte
        inner join @relations r on r.ParentID=cte.ChildId
        where cte.depth<32767
        and r.treeID=cte.treeID
    )
    select
    tree=case depth when 1 then cast(ParentID as varchar(30))+' (sort '+cast(SortOrder as varchar(30))+')'
        else REPLICATE(CHAR(9),depth-1)
            + cast(ChildId as varchar(30))+' (sort '+cast(SortOrder as varchar(30))+')'
        end
    from cte
    where depth>0
    order by agg
    option (maxrecursion 32767);
    go
    
    
    ---顺序排列Geovin Du PageSet,CategoryIsDisplay,CategoryPojectId
    
    WITH cte AS
    (
      SELECT 
        ID, 
        [ParentID],
        CategoryName,
    	PageSet,
        IdOrder,
        CAST(0 AS varbinary(max)) AS Level
      FROM Category
      WHERE [ParentID] is null and CategoryIsDisplay=1 and CategoryPojectId=1
      UNION ALL
      SELECT 
        i.ID, 
        i.[ParentID],
        i.CategoryName,
    	i.PageSet,
        i.IdOrder,  
        Level + CAST(i.ID AS varbinary(max)) AS Level
      FROM Category i
      INNER JOIN cte c
        ON c.ID = i.[ParentID]
    )
    SELECT 
      ID, 
      [ParentID],
      CategoryName,
      PageSet,
      IdOrder
    FROM cte
    ORDER BY [Level];
    go
    

      

    WITH cte AS
    (
      SELECT 
        ID, 
        [ParentID],
        CategoryName,
    	PageSet,
        IdOrder,
    	dbo.f_getlevel(ID) AS Dulv,
        CAST(0 AS varbinary(max)) AS Level	
      FROM Category
      WHERE [ParentID] is null and CategoryIsDisplay=1 and CategoryPojectId=1
      UNION ALL
      SELECT 
        i.ID, 
        i.[ParentID],
        i.CategoryName,
    	i.PageSet,
        i.IdOrder, 
    	dbo.f_getlevel(i.ID) AS Dulv,
        Level + CAST(i.ID AS varbinary(max)) AS Level	
      FROM Category i
      INNER JOIN cte c
        ON c.ID = i.[ParentID]
    )
    SELECT 
      ID, 
      [ParentID],
      CategoryName,
      PageSet,
      IdOrder,
      Dulv
    FROM cte
    ORDER BY [Level];
    go
    
    --
    WITH cte AS
    (
      SELECT 
        ID, 
        [ParentID],
        CategoryName,
    	PageSet,
        IdOrder,
        dbo.f_getlevel(ID) AS Level
      FROM Category
      WHERE [ParentID] is null and CategoryIsDisplay=1 and CategoryPojectId=1
      UNION ALL
      SELECT 
        i.ID, 
        i.[ParentID],
        i.CategoryName,
    	i.PageSet,
        i.IdOrder,  
        dbo.f_getlevel(i.ID) AS Level
      FROM Category i
      INNER JOIN cte c
        ON c.ID = i.[ParentID]
    )
    SELECT 
      ID, 
      [ParentID],
      CategoryName,
      PageSet,
      IdOrder,
      Level
    FROM cte
    ORDER BY Level;
    go
    

      

    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization
    关于gcc的一点小人性化提示
    python 命令行參数解析
    一起talk C栗子吧(第九回:C语言实例--最大公约数)
    小程序 通用请求
    小程序 上啦下拉刷新window配置
    微信小程序 功能函数 将对象的键添加到数组 (函数深入)
    微信小程序 功能函数 点击传参和页面
    微信小程序 功能函数 购物车商品删除
    微信 小程序组件 分页菜单带下划线焦点切换
  • 原文地址:https://www.cnblogs.com/geovindu/p/15308706.html
Copyright © 2011-2022 走看看