Note that there are some explanatory texts on larger screens.

plurals
  1. POCommon table expression from bottom-top approach
    primarykey
    data
    text
    <p>I have an Agent table and a hierarchy table.</p> <pre><code>CREATE TABLE [dbo].[Agent]( [AgentID] [int] IDENTITY(1,1) NOT NULL, [FirstName] [varchar](50) NULL, [LastName] [varchar](50) NULL, CONSTRAINT [PK_Agent] PRIMARY KEY CLUSTERED ( [AgentID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO CREATE TABLE [dbo].[Hierarchy]( [HierarchyID] [int] IDENTITY(1,1) NOT NULL, [AgentID] [int] NULL, [NextAgentID] [int] NULL, CONSTRAINT [PK_Hierarchy] PRIMARY KEY CLUSTERED ( [HierarchyID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO </code></pre> <p>--Insert to Agent</p> <pre><code>INSERT INTO [Agent]([FirstName],[LastName])VALUES('C1','C1'); INSERT INTO [Agent]([FirstName],[LastName])VALUES('C2','C2'); INSERT INTO [Agent]([FirstName],[LastName])VALUES('C3','C3'); INSERT INTO [Agent]([FirstName],[LastName])VALUES('C4','C4'); SELECT * FROM Agent; AgentID FirstName LastName 1 C1 C1 2 C2 C2 3 C3 C3 4 C4 C4 </code></pre> <p>--Insert to Hierarchy</p> <pre><code>INSERT INTO [Hierarchy] ([AgentID],[NextAgentID]) VALUES (1,NULL); INSERT INTO [Hierarchy] ([AgentID],[NextAgentID]) VALUES (2,1); INSERT INTO [Hierarchy] ([AgentID],[NextAgentID]) VALUES (3,2); INSERT INTO [Hierarchy] ([AgentID],[NextAgentID]) VALUES (2,4); INSERT INTO [Hierarchy] ([AgentID],[NextAgentID]) VALUES (4,NULL); SELECT * FROM Hierarchy; HierarchyID AgentID NextAgentID 1 1 NULL 2 2 1 3 3 2 4 2 4 5 4 NULL </code></pre> <p>I used a common table expression to determine the bottom to top levels</p> <pre><code>WITH AgentHierarchy(AgentID, NextAgentID, HierarchyLevel) AS ( SELECT H1.AgentID, H1.NextAgentID, 1 HierarchyLevel FROM Hierarchy H1 WHERE NOT EXISTS (SELECT 1 FROM Hierarchy H2 WHERE H2.NextAgentID = H1.AgentID) UNION ALL SELECT H.AgentID, H.NextAgentID, (AgentHierarchy.HierarchyLevel + 1) HierarchyLevel FROM Hierarchy H INNER JOIN AgentHierarchy ON AgentHierarchy.NextAgentID = H.AgentID ) SELECT DISTINCT AgentID, NextAgentID, HierarchyLevel FROM AgentHierarchy ORDER BY AgentID, NextAgentID, HierarchyLevel; </code></pre> <p>Result is:</p> <pre><code>AgentID NextAgentID HierarchyLevel 1 NULL 3 2 1 2 3 2 1 4 NULL 1 2 4 1 </code></pre> <p>My requirement is to show this in the below way:</p> <pre><code>AgentID NextAgentID HierarchyLevel 1 NULL 1 2 1 1 3 2 1 3 1 2 4 NULL 1 2 4 1 3 4 2 </code></pre> <p>In short, recursively all the hierarchy with levels should be pulled with bottom-to-top approach. Please help me...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload