自下而上方法的通用表表达式
我有一个代理表和一个层次结构表。
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
--Insert to Agent
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
--Insert to Hierarchy
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
我使用了一个通用表表达式来确定从下到上的级别
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;
结果是:
AgentID NextAgentID HierarchyLevel
1 NULL 3
2 1 2
3 2 1
4 NULL 1
2 4 1
我的要求是用以下方式显示这一点:
AgentID NextAgentID HierarchyLevel
1 NULL 1
2 1 1
3 2 1
3 1 2
4 NULL 1
2 4 1
3 4 2
简而言之,应递归地拉出所有具有级别的层次结构自下而上的方法。请帮我...
I have an Agent table and a hierarchy table.
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
--Insert to Agent
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
--Insert to Hierarchy
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
I used a common table expression to determine the bottom to top levels
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;
Result is:
AgentID NextAgentID HierarchyLevel
1 NULL 3
2 1 2
3 2 1
4 NULL 1
2 4 1
My requirement is to show this in the below way:
AgentID NextAgentID HierarchyLevel
1 NULL 1
2 1 1
3 2 1
3 1 2
4 NULL 1
2 4 1
3 4 2
In short, recursively all the hierarchy with levels should be pulled with bottom-to-top approach. Please help me...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了答案:
我做了以下更改:
最底层,NextAgentID 为 NULL。
如果有人有疑问请告诉我。
I found the answer:
I did the following changes:
bottom-most level with NULL NextAgentID.
Let me know if anyone has questions.