SQL 具有父 ID 和子 ID 的服务器层次结构

SQL Server hierarchy with parent id and child id

我想生成用红色表示的 'hierarchy id' 值。该值是层次结构中最低级别的 id。层次结构的最高级别是 'ALL Service Groups',最低级别是 'LL800_GB05'。它们与 Parent ID 和 ID 有关。所附图片仅为样本集。

更新--我已经建立了关系和血统。现在我知道 Hierarchy.I 的最高成员想要为新列中的每一行关联最低的 childID。我厌倦了在 Parentid 和 ID 上进行内部连接,但没有完全锻炼。我尝试了交叉连接但没有锻炼。下面是插入的数据

CREATE TABLE CO1 (
    PARENTID INT,
    ID INT,
    CID VARCHAR(38) ,
    Depth INT,
    Lineage VARCHAR(28) 
);
INSERT INTO CO1 VALUES (1105,48039,'All Service Groups',0,'/');
INSERT INTO CO1 VALUES (48039,48100,'Ovhd Service Groups',1,'/48039/');
INSERT INTO CO1 VALUES (48039,133686,'Network Services',1,'/48039/');
INSERT INTO CO1 VALUES (48039,133689,'Shared Services',1,'/48039/');
INSERT INTO CO1 VALUES (48039,133692,'SW Security K-12 Monitoring',1,'/48039/');
INSERT INTO CO1 VALUES (48039,133695,'Print Services Group',1,'/48039/');
INSERT INTO CO1 VALUES (48039,138170,'Miscellaneous Service Groups',1,'/48039/');
INSERT INTO CO1 VALUES (48039,48109,'Total Shared Services Security',1,'/48039/');
INSERT INTO CO1 VALUES (48100,140656,'K909_GB00',2,'/48039/48100/');
INSERT INTO CO1 VALUES (48100,140631,'K909_GG00',2,'/48039/48100/');
INSERT INTO CO1 VALUES (48100,140634,'K909_GA02',2,'/48039/48100/');
INSERT INTO CO1 VALUES (48100,48045,'G0000002',2,'/48039/48100/');
INSERT INTO CO1 VALUES (48109,48089,'GH010001',2,'/48039/48109/');
INSERT INTO CO1 VALUES (48109,48090,'GH010002',2,'/48039/48109/');
INSERT INTO CO1 VALUES (48109,48091,'GH010003',2,'/48039/48109/');
INSERT INTO CO1 VALUES (48109,48092,'GH010004',2,'/48039/48109/');
INSERT INTO CO1 VALUES (48109,48093,'GH010005',2,'/48039/48109/');
INSERT INTO CO1 VALUES (48109,48094,'GH010006',2,'/48039/48109/');
INSERT INTO CO1 VALUES (133686,133647,'Network Voice Services',2,'/48039/133686/');
INSERT INTO CO1 VALUES (133686,48106,'Network Data Services',2,'/48039/133686/');
INSERT INTO CO1 VALUES (133689,133634,'Server Hosting Services',2,'/48039/133689/');
INSERT INTO CO1 VALUES (133689,48105,'Mainframe Services',2,'/48039/133689/');
INSERT INTO CO1 VALUES (133689,133640,'Desktop Support & Application Services',2,'/48039/133689/');
INSERT INTO CO1 VALUES (133692,133682,'K12 Monitoring',2,'/48039/133692/');
INSERT INTO CO1 VALUES (133695,48108,'Total Print Services',2,'/48039/133695/');
INSERT INTO CO1 VALUES (133695,140641,'K909_GZ00',2,'/48039/133695/');
INSERT INTO CO1 VALUES (138170,138165,'Help Desk',2,'/48039/138170/');
INSERT INTO CO1 VALUES (138170,138166,'DS',2,'/48039/138170/');
INSERT INTO CO1 VALUES (138170,138167,'SCS',2,'/48039/138170/');
INSERT INTO CO1 VALUES (138170,138168,'IT Plan and Admin',2,'/48039/138170/');
INSERT INTO CO1 VALUES (138170,138162,'K909XGA01',2,'/48039/138170/');
INSERT INTO CO1 VALUES (138170,138163,'Surplus',2,'/48039/138170/');
INSERT INTO CO1 VALUES (133682,133681,'K909_GM00',3,'/48039/133692/133682/');
INSERT INTO CO1 VALUES (133682,133683,'K909_GM01',3,'/48039/133692/133682/');
INSERT INTO CO1 VALUES (133682,133684,'K909XGM01',3,'/48039/133692/133682/');
INSERT INTO CO1 VALUES (133647,133657,'Telecom Project Management',3,'/48039/133686/133647/');
INSERT INTO CO1 VALUES (133647,133660,'Telecom Wiring',3,'/48039/133686/133647/');
INSERT INTO CO1 VALUES (133647,133663,'Shared CENTREX',3,'/48039/133686/133647/');
INSERT INTO CO1 VALUES (133647,133649,'Total VoiP',3,'/48039/133686/133647/');
INSERT INTO CO1 VALUES (133647,133651,'Local Services Telco',3,'/48039/133686/133647/');
INSERT INTO CO1 VALUES (133647,133654,'Long Distance Telco',3,'/48039/133686/133647/');
INSERT INTO CO1 VALUES (133634,48076,'K909_GG40',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,48077,'K909_GG41',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,48078,'K909_GG42',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,48079,'K909_GG43',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,48080,'K909_GG44',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,48081,'K909_GG45',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,48082,'K909_GG46',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,48083,'K909_GG47',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,133638,'K909_GG48',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,133679,'K909_GG49',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,133680,'K909_GG07',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133634,138164,'K909_GG04',3,'/48039/133689/133634/');
INSERT INTO CO1 VALUES (133640,133642,'Application Services',3,'/48039/133689/133640/');
INSERT INTO CO1 VALUES (133640,133644,'Total Desktop Support',3,'/48039/133689/133640/');
INSERT INTO CO1 VALUES (48105,48067,'K909_GG11',3,'/48039/133689/48105/');
INSERT INTO CO1 VALUES (48105,48068,'K909_GG12',3,'/48039/133689/48105/');
INSERT INTO CO1 VALUES (48105,48069,'K909_GG13',3,'/48039/133689/48105/');
INSERT INTO CO1 VALUES (48105,48070,'K909_GG14',3,'/48039/133689/48105/');
INSERT INTO CO1 VALUES (48105,48071,'K909_GG10',3,'/48039/133689/48105/');
INSERT INTO CO1 VALUES (48106,133667,'Total DTO Internet',3,'/48039/133686/48106/');
INSERT INTO CO1 VALUES (48106,133670,'MetroNet',3,'/48039/133686/48106/');
INSERT INTO CO1 VALUES (48106,133673,'Router Management',3,'/48039/133686/48106/');
INSERT INTO CO1 VALUES (48106,133676,'Firll / VN Management',3,'/48039/133686/48106/');
INSERT INTO CO1 VALUES (48106,48103,'Total WAN',3,'/48039/133686/48106/');
INSERT INTO CO1 VALUES (48108,48095,'K909_GK00',3,'/48039/133695/48108/');
INSERT INTO CO1 VALUES (48103,48066,'K909_GD05',4,'/48039/133686/48106/48103/');
INSERT INTO CO1 VALUES (133642,48074,'K909_GG30',4,'/48039/133689/133640/133642/');
INSERT INTO CO1 VALUES (133644,133678,'K909_GG21',4,'/48039/133689/133640/133644/');
INSERT INTO CO1 VALUES (133644,48072,'K909_GG20',4,'/48039/133689/133640/133644/');
INSERT INTO CO1 VALUES (133649,48053,'K909_GB05',4,'/48039/133686/133647/133649/');
INSERT INTO CO1 VALUES (133651,48054,'K909_GB06',4,'/48039/133686/133647/133651/');
INSERT INTO CO1 VALUES (133654,48055,'K909_GB07',4,'/48039/133686/133647/133654/');
INSERT INTO CO1 VALUES (133657,48049,'K909_GB01',4,'/48039/133686/133647/133657/');
INSERT INTO CO1 VALUES (133657,133656,'K909_GB08',4,'/48039/133686/133647/133657/');
INSERT INTO CO1 VALUES (133660,133659,'K909_GB09',4,'/48039/133686/133647/133660/');
INSERT INTO CO1 VALUES (133660,48050,'K909_GB02',4,'/48039/133686/133647/133660/');
INSERT INTO CO1 VALUES (133663,48051,'K909_GB03',4,'/48039/133686/133647/133663/');
INSERT INTO CO1 VALUES (133663,48052,'K909_GB10',4,'/48039/133686/133647/133663/');
INSERT INTO CO1 VALUES (133667,48056,'K909_GD01',4,'/48039/133686/48106/133667/');
INSERT INTO CO1 VALUES (133667,48057,'K909_GD07',4,'/48039/133686/48106/133667/');
INSERT INTO CO1 VALUES (133670,48058,'K909_GD02',4,'/48039/133686/48106/133670/');
INSERT INTO CO1 VALUES (133673,48060,'K909_GD03',4,'/48039/133686/48106/133673/');
INSERT INTO CO1 VALUES (133676,48061,'K909_GD04',4,'/48039/133686/48106/133676/');
INSERT INTO CO1 VALUES (133676,48062,'K909_GD06',4,'/48039/133686/48106/133676/');

和SQL我试过的陈述如下

SELECT A.[PARENTID]
      ,A.[ID]
      ,A.[CID]
      ,A.[Depth]
      ,A.[Lineage],B.ID as HierID
  FROM [dbo].[CO1new] A

  inner join  [dbo].[CO1new] as B on (A.ID =B.PARENTID)

where A.PARENTID in ( 1105,133686,133647,133657,133660,133663,133649,133651,133654)

  order by 5

请输入任何信息

谢谢

如果您知道查询开头的最低后代,则向上遍历递归 cte,这将使这更容易,更简单。如果您不知道最低级别,那么您需要沿着树走下去才能找到它。所以你必须 know/be 能够找出顶部 parent 或底部后代。

这个答案有效,但只是因为最低级别只有 1 个后代,这对我来说似乎不太可能!但这就是问题所在,当最低级别有超过 1 个后代时,您如何选择将哪个 heirarchyid 放在 parent 中?或者您是否希望复制 parent 等,巧合的是,在这种情况下,此查询会执行此操作。

;WITH cteRecursive AS (
    SELECT
       GreatestAncestorId = Id
       ,Id
       ,ParentId
       ,Level = 0
    FROM
       @Table t
    WHERE
       ParentId = 1105

    UNION ALL

    SELECT
       c.GreatestAncestorId
       ,t.Id
       ,t.ParentId
       ,Level = c.Level + 1
    FROM
       @Table t
       INNER JOIN cteRecursive c
       ON t.ParentId = c.Id
)

, cteLowestDecendent AS (
    SELECT TOP 1 Id AS LowestDecendatId, GreatestAncestorId
    FROM
       cteRecursive
    ORDER BY
       Level DESC
)

SELECT`enter code here`
   l.LowestDecendatId
   ,r.*
FROM
    cteRecursive r
    INNER JOIN cteLowestDecendent l
   ON r.GreatestAncestorId = l.GreatestAncestorId

关于此 post 有一些有趣的技巧: 查看它们以了解有关在树上上下行走或一起创建不同类型的 heirarchyid 的更多详细信息。

我倾向于在层次结构中使用范围键。使用范围键(R1 和 R2),您可以 select and/or 聚合数据而无需递归查询。此外,您知道所有祖先 and/or 后代。现在,我通常使用范围键存储我的层次结构并根据需要重建。

此技术轻松支持可变深度(或锯齿状)层次结构。

在下面的示例中,通常顶部节点的父节点为 null,但样本数据没有,因此在锚查询中我有 where ParentID=1105

Declare @Table table (id int,ParentID int,Name varchar(50))
Insert Into @Table (ID,ParentID,Name) values
(48039,1105,'All Services'),
(133686,48039,'Ntw Services'),
(133647,133686,'Ntwk voice Services'),
(133649,133647,'Ntwk VOIP'),
(48053,133649,'LL800_GB05'),
(999999,133649,'Dummy to Illustrate multiple children')


Declare @GetID int = 48053   -- Any Desired ID

;With cteOH (id,ParentID,Lvl,Name,SortSeq) 
 as (
     Select id,ParentID,Lvl=1,Name,SortSeq = cast(concat(id,'>') as varchar(500)) from @Table where ParentID=1105
     Union All
     Select h.id,h.ParentID,cteOH.Lvl+1,h.Name ,SortSeq = cast(concat(cteOH.SortSeq,h.id)+'>' as varchar(500)) FROM @Table h INNER JOIN cteOH ON h.ParentID = cteOH.id 
    ),
    cteR1  as (Select id,SortSeq,r1=Row_Number() over (Order by SortSeq) From cteOH),
    cteR2  as (Select A.id,r2 = max(B.r1) From cteOH A Join cteR1 B on (B.SortSeq Like A.SortSeq+'%') Group By A.id)
    Select HierarchyID = F.ID  -- Added to to fullfill request
          ,B.R1                -- Included to Illustrate Range Keys
          ,C.R2                -- Included to Illustrate Range Keys
          ,A.Lvl               -- Included to Illustrate Level of Node
          ,A.ID
          ,A.ParentID
          ,A.Name
     From  cteOH A
     Join  cteR1 B on (A.ID=B.ID)
     Join  cteR2 C on (A.ID=C.ID)
     Join  cteR1 F on (F.ID=@GetID and F.R1 between B.R1 and C.R2)

Returns

HierarchyID R1  R2  Lvl ID      ParentID    Name
48053       1   6   1   48039   1105        All Services
48053       2   6   2   133686  48039       Ntw Services
48053       3   6   3   133647  133686      Ntwk voice Services
48053       4   6   4   133649  133647      Ntwk VOIP
48053       5   5   5   48053   133649      LL800_GB05

请注意我的虚拟 ID 未显示

现在在这个示例中,我加入了 Join cteR1 F 这样你就可以返回层次结构

@Matt 指出我误解了请求,所以我想找个机会来弥补自己并介绍 Range Key 技术。

简化的滚动到顶部

Declare @Table table (id int,ParentID int,Name varchar(50))
Insert Into @Table (ID,ParentID,Name) values
(48039,1105,'All Services'),
(133686,48039,'Ntw Services'),
(133647,133686,'Ntwk voice Services'),
(133649,133647,'Ntwk VOIP'),
(48053,133649,'LL800_GB05')


Declare @GetID int = 48053   -- Any Desired ID


;With cteOH (Lvl,HierarchyId,id,ParentID,Name) 
  as (
      Select Lvl=0,HierarchyId=@GetID,id,ParentID,Name from @Table where ID=@GetID
      Union All
      Select cteOH.Lvl-1,HierarchyId=@GetID,h.id,h.ParentID,h.Name FROM @Table h INNER JOIN cteOH ON cteOH.ParentID = H.id 
     )
     Select HierarchyId,id,ParentID,Name from cteOH Order by Lvl

Returns

HierarchyId id      ParentID    Name
48053       48039   1105        All Services
48053       133686  48039       Ntw Services
48053       133647  133686      Ntwk voice Services
48053       133649  133647      Ntwk VOIP
48053       48053   133649      LL800_GB05

当我 运行 基于您提供的数据构建 FULL Hier 时,结果立竿见影,如下所示:

R1                   R2                   Lvl         ID          ParentID    CID
-------------------- -------------------- ----------- ----------- ----------- --------------------------------------
1                    85                   1           48039       1105        All Service Groups
2                    31                   2           133686      48039       Network Services
3                    18                   3           133647      133686      Network Voice Services
4                    5                    4           133649      133647      Total VoiP
5                    5                    5           48053       133649      K909_GB05
6                    7                    4           133651      133647      Local Services Telco
7                    7                    5           48054       133651      K909_GB06
8                    9                    4           133654      133647      Long Distance Telco
9                    9                    5           48055       133654      K909_GB07
10                   12                   4           133657      133647      Telecom Project Management
11                   11                   5           133656      133657      K909_GB08
12                   12                   5           48049       133657      K909_GB01
13                   15                   4           133660      133647      Telecom Wiring
14                   14                   5           133659      133660      K909_GB09
15                   15                   5           48050       133660      K909_GB02
16                   18                   4           133663      133647      Shared CENTREX
17                   17                   5           48051       133663      K909_GB03
18                   18                   5           48052       133663      K909_GB10
19                   31                   3           48106       133686      Network Data Services
20                   22                   4           133667      48106       Total DTO Internet
21                   21                   5           48056       133667      K909_GD01
22                   22                   5           48057       133667      K909_GD07
23                   24                   4           133670      48106       MetroNet
24                   24                   5           48058       133670      K909_GD02
25                   26                   4           133673      48106       Router Management
26                   26                   5           48060       133673      K909_GD03
27                   29                   4           133676      48106       Firll / VN Management
28                   28                   5           48061       133676      K909_GD04
29                   29                   5           48062       133676      K909_GD06
30                   31                   4           48103       48106       Total WAN
31                   31                   5           48066       48103       K909_GD05
32                   57                   2           133689      48039       Shared Services
33                   45                   3           133634      133689      Server Hosting Services
34                   34                   4           133638      133634      K909_GG48
35                   35                   4           133679      133634      K909_GG49
36                   36                   4           133680      133634      K909_GG07
37                   37                   4           138164      133634      K909_GG04
38                   38                   4           48076       133634      K909_GG40
39                   39                   4           48077       133634      K909_GG41
40                   40                   4           48078       133634      K909_GG42
41                   41                   4           48079       133634      K909_GG43
42                   42                   4           48080       133634      K909_GG44
43                   43                   4           48081       133634      K909_GG45
44                   44                   4           48082       133634      K909_GG46
45                   45                   4           48083       133634      K909_GG47
46                   51                   3           133640      133689      Desktop Support & Application Services
47                   48                   4           133642      133640      Application Services
48                   48                   5           48074       133642      K909_GG30
49                   51                   4           133644      133640      Total Desktop Support
50                   50                   5           133678      133644      K909_GG21
51                   51                   5           48072       133644      K909_GG20
52                   57                   3           48105       133689      Mainframe Services
53                   53                   4           48067       48105       K909_GG11
54                   54                   4           48068       48105       K909_GG12
55                   55                   4           48069       48105       K909_GG13
56                   56                   4           48070       48105       K909_GG14
57                   57                   4           48071       48105       K909_GG10
58                   62                   2           133692      48039       SW Security K-12 Monitoring
59                   62                   3           133682      133692      K12 Monitoring
60                   60                   4           133681      133682      K909_GM00
61                   61                   4           133683      133682      K909_GM01
62                   62                   4           133684      133682      K909XGM01
63                   66                   2           133695      48039       Print Services Group
64                   64                   3           140641      133695      K909_GZ00
65                   66                   3           48108       133695      Total Print Services
66                   66                   4           48095       48108       K909_GK00
67                   73                   2           138170      48039       Miscellaneous Service Groups
68                   68                   3           138162      138170      K909XGA01
69                   69                   3           138163      138170      Surplus
70                   70                   3           138165      138170      Help Desk
71                   71                   3           138166      138170      DS
72                   72                   3           138167      138170      SCS
73                   73                   3           138168      138170      IT Plan and Admin
74                   78                   2           48100       48039       Ovhd Service Groups
75                   75                   3           140631      48100       K909_GG00
76                   76                   3           140634      48100       K909_GA02
77                   77                   3           140656      48100       K909_GB00
78                   78                   3           48045       48100       G0000002
79                   85                   2           48109       48039       Total Shared Services Security
80                   80                   3           48089       48109       GH010001
81                   81                   3           48090       48109       GH010002
82                   82                   3           48091       48109       GH010003
83                   83                   3           48092       48109       GH010004
84                   84                   3           48093       48109       GH010005
85                   85                   3           48094       48109       GH010006

(85 row(s) affected)

根据您的要求。这是构建完整层次结构的代码。
此结果是根据您在问题中提供的 85 行层次结构构建的。

我还是不明白你所说的最低级别是什么意思。有许多最低级别。我添加了一个 IsParent 字段,它指示一个点是否是 parent/roll-up...零表示叶节点或底部节点。

;With cteOH (id,ParentID,Lvl,CID,SortSeq) 
 as (
     Select id,ParentID,Lvl=1,CID,SortSeq = cast(concat(id,'>') as varchar(500)) from CO1 where ParentID=1105
     Union All
     Select h.id,h.ParentID,cteOH.Lvl+1,h.CID ,SortSeq = cast(concat(cteOH.SortSeq,h.id)+'>' as varchar(500)) FROM CO1 h INNER JOIN cteOH ON h.ParentID = cteOH.id 
    ),
    cteR1  as (Select id,SortSeq,r1=Row_Number() over (Order by SortSeq) From cteOH),
    cteR2  as (Select A.id,r2 = max(B.r1) From cteOH A Join cteR1 B on (B.SortSeq Like A.SortSeq+'%') Group By A.id)
    Select B.R1
          ,C.R2
          ,A.Lvl
          ,A.ID
          ,A.ParentID
          ,A.CID
          ,IsParent = iif(R1<>R2,1,0)
     Into  dbo.CO1_Hier                 -- << Storing Results of Hier For Ease of Use
     From  cteOH A
     Join  cteR1 B on (A.ID=B.ID)
     Join  cteR2 C on (A.ID=C.ID)

层次结构构建结果

R1                   R2                   Lvl         ID          ParentID    CID                                    IsParent
-------------------- -------------------- ----------- ----------- ----------- -------------------------------------- -----------
1                    85                   1           48039       1105        All Service Groups                     1
2                    31                   2           133686      48039       Network Services                       1
3                    18                   3           133647      133686      Network Voice Services                 1
4                    5                    4           133649      133647      Total VoiP                             1
5                    5                    5           48053       133649      K909_GB05                              0
6                    7                    4           133651      133647      Local Services Telco                   1
7                    7                    5           48054       133651      K909_GB06                              0
8                    9                    4           133654      133647      Long Distance Telco                    1
9                    9                    5           48055       133654      K909_GB07                              0
10                   12                   4           133657      133647      Telecom Project Management             1
11                   11                   5           133656      133657      K909_GB08                              0
12                   12                   5           48049       133657      K909_GB01                              0
13                   15                   4           133660      133647      Telecom Wiring                         1
14                   14                   5           133659      133660      K909_GB09                              0
15                   15                   5           48050       133660      K909_GB02                              0
16                   18                   4           133663      133647      Shared CENTREX                         1
17                   17                   5           48051       133663      K909_GB03                              0
18                   18                   5           48052       133663      K909_GB10                              0
19                   31                   3           48106       133686      Network Data Services                  1
20                   22                   4           133667      48106       Total DTO Internet                     1
21                   21                   5           48056       133667      K909_GD01                              0
22                   22                   5           48057       133667      K909_GD07                              0
23                   24                   4           133670      48106       MetroNet                               1
24                   24                   5           48058       133670      K909_GD02                              0
25                   26                   4           133673      48106       Router Management                      1
26                   26                   5           48060       133673      K909_GD03                              0
27                   29                   4           133676      48106       Firll / VN Management                  1
28                   28                   5           48061       133676      K909_GD04                              0
29                   29                   5           48062       133676      K909_GD06                              0
30                   31                   4           48103       48106       Total WAN                              1
31                   31                   5           48066       48103       K909_GD05                              0
32                   57                   2           133689      48039       Shared Services                        1
33                   45                   3           133634      133689      Server Hosting Services                1
34                   34                   4           133638      133634      K909_GG48                              0
35                   35                   4           133679      133634      K909_GG49                              0
36                   36                   4           133680      133634      K909_GG07                              0
37                   37                   4           138164      133634      K909_GG04                              0
38                   38                   4           48076       133634      K909_GG40                              0
39                   39                   4           48077       133634      K909_GG41                              0
40                   40                   4           48078       133634      K909_GG42                              0
41                   41                   4           48079       133634      K909_GG43                              0
42                   42                   4           48080       133634      K909_GG44                              0
43                   43                   4           48081       133634      K909_GG45                              0
44                   44                   4           48082       133634      K909_GG46                              0
45                   45                   4           48083       133634      K909_GG47                              0
46                   51                   3           133640      133689      Desktop Support & Application Services 1
47                   48                   4           133642      133640      Application Services                   1
48                   48                   5           48074       133642      K909_GG30                              0
49                   51                   4           133644      133640      Total Desktop Support                  1
50                   50                   5           133678      133644      K909_GG21                              0
51                   51                   5           48072       133644      K909_GG20                              0
52                   57                   3           48105       133689      Mainframe Services                     1
53                   53                   4           48067       48105       K909_GG11                              0
54                   54                   4           48068       48105       K909_GG12                              0
55                   55                   4           48069       48105       K909_GG13                              0
56                   56                   4           48070       48105       K909_GG14                              0
57                   57                   4           48071       48105       K909_GG10                              0
58                   62                   2           133692      48039       SW Security K-12 Monitoring            1
59                   62                   3           133682      133692      K12 Monitoring                         1
60                   60                   4           133681      133682      K909_GM00                              0
61                   61                   4           133683      133682      K909_GM01                              0
62                   62                   4           133684      133682      K909XGM01                              0
63                   66                   2           133695      48039       Print Services Group                   1
64                   64                   3           140641      133695      K909_GZ00                              0
65                   66                   3           48108       133695      Total Print Services                   1
66                   66                   4           48095       48108       K909_GK00                              0
67                   73                   2           138170      48039       Miscellaneous Service Groups           1
68                   68                   3           138162      138170      K909XGA01                              0
69                   69                   3           138163      138170      Surplus                                0
70                   70                   3           138165      138170      Help Desk                              0
71                   71                   3           138166      138170      DS                                     0
72                   72                   3           138167      138170      SCS                                    0
73                   73                   3           138168      138170      IT Plan and Admin                      0
74                   78                   2           48100       48039       Ovhd Service Groups                    1
75                   75                   3           140631      48100       K909_GG00                              0
76                   76                   3           140634      48100       K909_GA02                              0
77                   77                   3           140656      48100       K909_GB00                              0
78                   78                   3           48045       48100       G0000002                               0
79                   85                   2           48109       48039       Total Shared Services Security         1
80                   80                   3           48089       48109       GH010001                               0
81                   81                   3           48090       48109       GH010002                               0
82                   82                   3           48091       48109       GH010003                               0
83                   83                   3           48092       48109       GH010004                               0
84                   84                   3           48093       48109       GH010005                               0
85                   85                   3           48094       48109       GH010006                               0

(85 row(s) affected)][1]][1]

Now, this is the cool part!

** 我在 NON-PARENT 或 table Co1_Trans

中的叶级别创建了一些随机数据

以下查询说明了如何通过范围键汇总数据

Select H.R1
      ,H.R2
      ,H.Lvl
      ,H.ID
      ,H.ParentID
      ,H.CID
      ,H.IsParent
      ,NestedDesc      = space((H.Lvl-1)*2)+H.CID
      ,Records         = count(*)
      ,MinVal          = min(SomeValue)
      ,MaxVal          = max(SomeValue)
      ,AvgVal          = Avg(SomeValue)
      ,SomeValue       = sum(SomeValue)
      ,SomeOtherValue  = sum(SomeOtherValue)
 From dbo.CO1_Hier H
 Join (Select _R1=B.R1,A.* From [dbo].[CO1_Trans] A Join dbo.CO1_Hier B on (A.ID=B.ID)) B
   on (B._R1 between H.R1 and H.R2)
 Group By H.R1
      ,H.R2
      ,H.Lvl
      ,H.ID
      ,H.ParentID
      ,H.CID
      ,H.IsParent
  Order By H.R1

这里显示结果

现在,假设您只想显示级别 1 和级别 2。让我们使用与之前相同的查询,但添加一个 where 子句.. Where H.Lvl<3

      ...
      ,SomeOtherValue  = sum(SomeOtherValue)
 From dbo.CO1_Hier H
 Join (Select _R1=B.R1,A.* From [dbo].[CO1_Trans] A Join dbo.CO1_Hier B on (A.ID=B.ID)) B  on (B._R1 between H.R1 and H.R2)
 Where H.Lvl<3
 Group By H.R1
      ,H.R2
      ,H.Lvl
      ...