问题
I would like to generate the 'hierarchy id' values which is denoted in red. The value is the lowest level of id in the hierarchy. The highest level of hierarchy is 'ALL Service Groups' and the lowest level is 'LL800_GB05'. They are related with Parent ID and ID. The attached image is only a sample set.
Update-- I have formed a relationship and lineage. Now i know the highest member of Hierarchy.I want to associate the lowest childID for each row in a new column. I tired doing an inner join on Parentid and ID but didnot workout completely. I tried a crossjoin but didnot workout. Below is the data for insertion
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/');
and SQL statements i tried are below
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
Any inputs please
Thanks
回答1:
As per your request. Here is the code to build the full hierarchy.
This results were constructed from the 85 rows of hierarchy that you supplied in your question.
I still don't know what you mean by Lowest Level. There are many lowest levels. I added a field IsParent which indicates whether or not a point is a parent/roll-up... Zero indicates a leaf or bottom node.
;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)
Results of Hierarchy Build
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!
** I created some random data at the NON-PARENT or leaf level in table Co1_Trans
The following query illustrates how to roll the data up via the range keys
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
The results are displayed here
Now, let's say that you only wanted to show Levels 1 and 2. Let's use the same query as before, but add a where clause .. 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
...
回答2:
If you know the lowest descendant at the beginning of your query then walk up through a recursive cte which would make this easier and more fool proof. If you don't know the lowest level then you need to walk down the tree to find it. So you have to know/be able to figure out either the top parent or the bottom descendant.
This answer works but only because there is only 1 descendant at the lowest level which seems very unlikely to me! But that is the issue, how would you choose which heirarchyid to put at the parent when there is more than 1 descendant at the lowest level? Or would you want the parent etc. to be duplicated which coincidentally this query will do in that case.
;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
There are a couple of interesting techniques on this post: SQL Hierarchy - Resolve full path for all ancestors of a given node check them out for more detail on walking up and down the tree or creating a different type of heirarchyid all together.
回答3:
I tend to use range keys on my hierarchies. With the range keys (R1 & R2), you can select and/or aggregate data without the need for recursive queries. Also, you know all ancestors and/or descendants. Now, I normally store my hierarchies with the ranges keys and rebuild as necessary.
This technique supports variable depth (or jagged) hierarchies with ease.
In the following example, normally the top node(s) have a parent of null, but the sample data did not, so in the anchor query I have 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
Notice my dummy ID is not displayed
Now in this sample, I included Join cteR1 F so you can go Back UP the hierarchy
@Matt pointed out that I misunderstood the request, so I wanted a chance to redeem myself and introduce the the Range Key technique.
回答4:
A simplified Roll-To-Top
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
When I ran the FULL Hier Build on your data supplied, the results were immediate and as follows:
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)
来源:https://stackoverflow.com/questions/37954697/sql-server-hierarchy-with-parent-id-and-child-id