SQL CTE将层次结构作为串联结果
在下面的示例中,我试图检索每个员工的完整报告结构。
例如,1向10报告。 10向100报告。 100向1000报告。 1000向10000报告。 10000正在向首席执行官报告 首席执行官的报告不再是级别,
因此对于人1,完整的报告结构shoule be | 10 | 100 | 1000 | 10000 |首席执行官|不再级别|
这样,我需要所有人的完整报告结构。
我的数据
Person AssignedTo
1 10
2 20
3 30
10 100
20 200
30 300
100 1000
200 2000
300 3000
1000 10000
2000 20000
3000 30000
10000 CEO
20000 CEO
30000 CEO
CEO No More Level
预期结果
|10|100|1000|10000|CEO|No More Level|
|20|200|2000|20000|CEO|No More Level|
|30|300|3000|30000|CEO|No More Level|
|100|1000|10000|CEO|No More Level|
|200|2000|20000|CEO|No More Level|
|300|3000|30000|CEO|No More Level|
|1000|10000|CEO|No More Level|
|2000|20000|CEO|No More Level|
|3000|30000|CEO|No More Level|
|10000|CEO|No More Level|
|20000|CEO|No More Level|
|30000|CEO|No More Level|
|CEO|No More Level|
|CEO|No More Level|
|CEO|No More Level|
|No More Level|
我尝试过,但它不起作用。 sqlfiddlelink
插入表语句
CREATE TABLE [dbo].[tblHierarchy](
[Person] [nvarchar](50) NULL,
[AssignedTo] [nvarchar](50) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'1', N'10')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'2', N'20')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'3', N'30')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'10', N'100')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'20', N'200')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'30', N'300')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'100', N'1000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'200', N'2000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'300', N'3000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'1000', N'10000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'2000', N'20000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'3000', N'30000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'10000', N'CEO')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'20000', N'CEO')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'30000', N'CEO')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'CEO', N'No More Level')
GO
我的SQL尝试
with cte as (
select Person, cast(Person as nvarchar(max)) caption,
AssignedTo from tblHierarchy
union all
select t.Person, cast('|'+ c.caption + '|' + t.Person as nvarchar(max))+'|', t.AssignedTo
from cte c
inner join tblHierarchy t on t.AssignedTo = c.Person
)
select Person,caption from cte order by Person
In the below example I am trying to retrieve the complete reporting structure of each employees.
For example 1 is reporting to 10.
10 is reporting to 100.
100 is reporting to 1000.
1000 is reporting to 10000.
10000 is reporting to CEO
CEO is reporting to No More Level
so for person 1 the complete reporting structure shoule be |10|100|1000|10000|CEO|No More Level|
Like this I require the complete reporting structure for all persons.
My Data
Person AssignedTo
1 10
2 20
3 30
10 100
20 200
30 300
100 1000
200 2000
300 3000
1000 10000
2000 20000
3000 30000
10000 CEO
20000 CEO
30000 CEO
CEO No More Level
Expected Result
|10|100|1000|10000|CEO|No More Level|
|20|200|2000|20000|CEO|No More Level|
|30|300|3000|30000|CEO|No More Level|
|100|1000|10000|CEO|No More Level|
|200|2000|20000|CEO|No More Level|
|300|3000|30000|CEO|No More Level|
|1000|10000|CEO|No More Level|
|2000|20000|CEO|No More Level|
|3000|30000|CEO|No More Level|
|10000|CEO|No More Level|
|20000|CEO|No More Level|
|30000|CEO|No More Level|
|CEO|No More Level|
|CEO|No More Level|
|CEO|No More Level|
|No More Level|
I tried this but it is not working.
SQLFiddleLink
Insert Table statement
CREATE TABLE [dbo].[tblHierarchy](
[Person] [nvarchar](50) NULL,
[AssignedTo] [nvarchar](50) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'1', N'10')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'2', N'20')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'3', N'30')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'10', N'100')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'20', N'200')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'30', N'300')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'100', N'1000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'200', N'2000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'300', N'3000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'1000', N'10000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'2000', N'20000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'3000', N'30000')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'10000', N'CEO')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'20000', N'CEO')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'30000', N'CEO')
GO
INSERT [dbo].[tblHierarchy] ([Person], [AssignedTo]) VALUES (N'CEO', N'No More Level')
GO
My SQL Attempt
with cte as (
select Person, cast(Person as nvarchar(max)) caption,
AssignedTo from tblHierarchy
union all
select t.Person, cast('|'+ c.caption + '|' + t.Person as nvarchar(max))+'|', t.AssignedTo
from cte c
inner join tblHierarchy t on t.AssignedTo = c.Person
)
select Person,caption from cte order by Person
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
的第一个中使用过滤器
您可以尝试在CTE 查询1
: 结果 :
You can try to use a filter in first of cte
Query 1:
Results:
似乎这就是您所追求的。您需要使用RCTE通过数据集进行迭代,并为“原始”用户及其“父”提供详细信息。然后,您需要根据最大级别为每个用户获得每个用户的“每组最高1”,因为否则您将获得每个级别的1行。然后,最后,您可以在行号上过滤:
请注意,由于您的数据类型选择,订购不是预期结果中的内容。
person
是nvarchar
,因此值'10'
是 低 ,因此,将首先排序以'1'
开始的所有人,然后是'2'
,然后'3'
代码>,等Seems this is what you're after. you need to use an rCTE to iterate through your dataset, providing details for both the "original" user and their "parent". Then you need to get the "Top 1 per group" for each user, based on the maximum level, as you'll get 1 row per level for a user otherwise. Then, finally, you can filter on the row number:
Note that that the ordering isn't what is in your expected results due to your data type choice.
Person
is annvarchar
and so the value'10'
is lower than the value'2'
, and so all person's with a value starting with'1'
will be sorted first, then'2'
, then'3'
, etc.也许这就是您所追求的?
在此
”
Maybe this is what you are after ?
Try it in this DBFiddle
The result is