Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

sql - How to create query from parent child hierarchy table

How do I write my query to translate a table with parent / child hierarchy into a table with my hierarchy levels in separate columns?

I have a table in SQL Server (from SAP without any changes made I believe) that gives me the structure of groups containing my profit centers. The structure of the table is a classic parent child hierarchy as shown below.

Parent  |  Child  
--------+--------
S-1     |  S-11  
S-1     |  S-12  
S-1     |  S-13  
S-1     |  S-14  
S-1     |  S-15  
S-11    |  S-111  
S-11    |  S-112  
..      |  ..  
S-152   |  S-1521  
S-152   |  S-1522  
S-1522  |  S-15221  

I want to write a query that gives me a table where I for each group can find the Level 1, Level 2, Level 3 ect. group. Level 1 is the Top Level (and will always exist) and Level 2 the next. There can be unlimited levels but at this time Level 8 is the highest used.

Group   | Level 1   | Level 2   | Level 3   | Level 4   | Level 5  
--------+-----------+-----------+-----------+-----------+---------
S-111   | S-1       | S-11      | S-111     |           |  
S-11211 | S-1       | S-11      | S-112     | S-1121    | S-11211  
S-1211  | S-1       | S-12      | S-121     | S-1211    | 
S-1212  | S-1       | S-12      | S-121     | S-1212    | 
S-122   | S-1       | S-12      | S-122     |           |     
S-123   | S-1       | S-12      | S-123     |           |
S-1311  | S-1       | S-13      | S-131     | S-1311    | 
S-1312  | S-1       | S-13      | S-131     | S-1312    | 
S-1321  | S-1       | S-13      | S-132     | S-1321    | 
S-141   | S-1       | S-14      | S-141     |           |
S-151   | S-1       | S-15      | S-151     |           |
S-1521  | S-1       | S-15      | S-152     | S-1521    | 
S-15221 | S-1       | S-15      | S-152     | S-1522    | S-15221  

I have used Google and this page to find the final solutions but haven’t found it yet. But I managed to get this far:

WITH MyTest as  
(  
    SELECT 
        P.PRCTR_CHILD, P.PRCTR_PARENT, 
        CAST(P.PRCTR_CHILD AS VARCHAR(MAX)) AS Level  
    FROM 
        [IBM_PA_Integration].[dbo].[PRCTRHIER] AS P  
    WHERE 
        P.PRCTR_PARENT = 'S-1000' –- S-1000 is a division  

    UNION ALL  

    SELECT 
        P1.PRCTR_CHILD, P1.PRCTR_PARENT, 
        CAST(P1.PRCTR_CHILD AS VARCHAR(MAX)) + ', ' + M.Level  
    FROM 
        [IBM_PA_Integration].[dbo].[PRCTRHIER] AS P1  
    INNER JOIN 
        MyTest M ON M.PRCTR_CHILD = P1.PRCTR_PARENT  
)  
SELECT *   
FROM MyTest  
WHERE PRCTR_PARENT  = 'FS2004' –- FS2004 is the level top level / level above S-1000  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have a fixed or limited number of levels, you may not need DYNAMIC SQL. "Parsing" the path can be accomplished with a little XML.

Consider the following:

Example:

Declare @YourTable Table ([Parent] varchar(50),[Child] varchar(50))
Insert Into @YourTable Values 
 (null ,'S-1')
,('S-1','S-11')
,('S-1','S-12')
,('S-1','S-13')
,('S-1','S-14')
,('S-1','S-15')
,('S-11','S-111')
,('S-11','S-112')

;with cteP as (
      Select Child
            ,Parent 
            ,PathID = cast(Child as varchar(500))
      From   @YourTable
      Where  Parent is Null
      Union  All
      Select Child  = r.Child
            ,Parent = r.Parent 
            ,PathID = cast(p.PathID+','+cast(r.Child as varchar(25)) as varchar(500))
      From   @YourTable r
      Join   cteP p on r.Parent  = p.Child)
Select [Group] = Child
      ,B.*
 From  cteP A
 Cross Apply (
                Select Level1 = xDim.value('/x[1]','varchar(max)')
                      ,Level2 = xDim.value('/x[2]','varchar(max)')
                      ,Level3 = xDim.value('/x[3]','varchar(max)')
                      ,Level4 = xDim.value('/x[4]','varchar(max)')
                      ,Level5 = xDim.value('/x[5]','varchar(max)')
                From  (Select Cast('<x>' + replace(PathID,',','</x><x>')+'</x>' as xml) as xDim) as X 
             ) B
  Order By PathID

Returns

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...