层次问题

发布于 2024-09-24 22:40:21 字数 342 浏览 2 评论 0原文

我有一个带有 hierarchyid 列的表。就像:

[NAME] [PATH]
Ahmet /
Aliye /1/
Selen /1/1/
Erdem /2/
Bilge /2/1/
Aydin /2/2/
Tomrs /2/2/2/

我想看到类似的名字:

[NAMES_WITH_HIERARCHY]
Ahmet
Ahmet/Aliye
Ahmet/Aliye/Selen
Ahmet/Erdem
Ahmet/Erdem/Bilge
Ahmet/Erdem/Aydin
Ahmet/Erdem/Aydin/Tomrs

我该怎么做?

I have a table with hierarchyid column. It is like:

[NAME] [PATH]
Ahmet /
Aliye /1/
Selen /1/1/
Erdem /2/
Bilge /2/1/
Aydin /2/2/
Tomrs /2/2/2/

I want to see NAMES like:

[NAMES_WITH_HIERARCHY]
Ahmet
Ahmet/Aliye
Ahmet/Aliye/Selen
Ahmet/Erdem
Ahmet/Erdem/Bilge
Ahmet/Erdem/Aydin
Ahmet/Erdem/Aydin/Tomrs

How can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-10-01 22:40:21

给你:

declare @hierarchy table (name varchar(20), [path] hierarchyid)
insert into @hierarchy ( name, path )
values  
 ('Ahmet', '/')
,('Aliye', '/1/')
,('Selen', '/1/1/')
,('Erdem', '/2/')
,('Bilge', '/2/1/')
,('Aydin', '/2/2/')
,('Tomrs', '/2/2/2/')

--select * from @hierarchy as h

;with Tree([level], [FullName], [path]) as (
    select h.[path].GetLevel() as [level], cast(h.[name] as varchar(max)), h.[path]
    from @hierarchy as h
    where [path] = '/'
    union all
    select h2.[path].GetLevel(), t.[FullName] + '/' + h2.[name] , h2.[path]
    from Tree t
    join @hierarchy as h2 on h2.[path].IsDescendantOf(t.[path]) = 1 and t.[path] <> h2.[path] and h2.[path].GetLevel() - t.[level] < 2 
)
select [Level], cast(FullName as varchar(25)) [Fullname], cast(Path as varchar(10)) [Path] 
from Tree
order by Path

输出:

Level  Fullname                  Path
------ ------------------------- ----------
0      Ahmet                     /
1      Ahmet/Aliye               /1/
2      Ahmet/Aliye/Selen         /1/1/
1      Ahmet/Erdem               /2/
2      Ahmet/Erdem/Bilge         /2/1/
2      Ahmet/Erdem/Aydin         /2/2/
3      Ahmet/Erdem/Aydin/Tomrs   /2/2/2/

Here you go:

declare @hierarchy table (name varchar(20), [path] hierarchyid)
insert into @hierarchy ( name, path )
values  
 ('Ahmet', '/')
,('Aliye', '/1/')
,('Selen', '/1/1/')
,('Erdem', '/2/')
,('Bilge', '/2/1/')
,('Aydin', '/2/2/')
,('Tomrs', '/2/2/2/')

--select * from @hierarchy as h

;with Tree([level], [FullName], [path]) as (
    select h.[path].GetLevel() as [level], cast(h.[name] as varchar(max)), h.[path]
    from @hierarchy as h
    where [path] = '/'
    union all
    select h2.[path].GetLevel(), t.[FullName] + '/' + h2.[name] , h2.[path]
    from Tree t
    join @hierarchy as h2 on h2.[path].IsDescendantOf(t.[path]) = 1 and t.[path] <> h2.[path] and h2.[path].GetLevel() - t.[level] < 2 
)
select [Level], cast(FullName as varchar(25)) [Fullname], cast(Path as varchar(10)) [Path] 
from Tree
order by Path

Output:

Level  Fullname                  Path
------ ------------------------- ----------
0      Ahmet                     /
1      Ahmet/Aliye               /1/
2      Ahmet/Aliye/Selen         /1/1/
1      Ahmet/Erdem               /2/
2      Ahmet/Erdem/Bilge         /2/1/
2      Ahmet/Erdem/Aydin         /2/2/
3      Ahmet/Erdem/Aydin/Tomrs   /2/2/2/
皓月长歌 2024-10-01 22:40:21

这个答案也帮助了我。我想我会添加它发现的改进。
将性能从 3 分钟更改

join @hierarchy as h2 on h2.[path].IsDescendantOf(t.[path]) = 1 and 
                         t.[path] <> h2.[path] and 
                         h2.[path].GetLevel() - t.[level] < 2

JOIN @hierarchy AS h2 ON h2.[path].GetAncestor(1) = t.[path]

2 秒,无论索引如何,这都使其与自引用相当。

This answer helped me out too. Thought I would add an improvement it found.
Changing

join @hierarchy as h2 on h2.[path].IsDescendantOf(t.[path]) = 1 and 
                         t.[path] <> h2.[path] and 
                         h2.[path].GetLevel() - t.[level] < 2

to

JOIN @hierarchy AS h2 ON h2.[path].GetAncestor(1) = t.[path]

increased performance from 3 min to 2 sec, this put it on par with self-referencing regardless of indexing.

忆沫 2024-10-01 22:40:21

您不必使用递归 CTE (Denis Valeev) 的答案,它的性能较低。只需将其与自己连接

DECLARE @hierarchy TABLE (name VARCHAR(20), [path] HIERARCHYID)
INSERT INTO @hierarchy (name, path)
VALUES  
 ('Ahmet', '/')
,('Aliye', '/1/')
,('Selen', '/1/1/')
,('Erdem', '/2/')
,('Bilge', '/2/1/')
,('Aydin', '/2/2/')
,('Tomrs', '/2/2/2/')

--SELECT * FROM @hierarchy AS h

SELECT
    path.GetLevel() As Level,
    (
        SELECT STRING_AGG(A.name, '/')
        FROM
            @hierarchy A
        WHERE
            B.path.IsDescendantOf(A.path) = 1               
    ) AS FullName,
    path.ToString() AS Path
FROM
    @hierarchy B

即可输出:

Level  Fullname                  Path
------ ------------------------- ----------
0      Ahmet                     /
1      Ahmet/Aliye               /1/
2      Ahmet/Aliye/Selen         /1/1/
1      Ahmet/Erdem               /2/
2      Ahmet/Erdem/Bilge         /2/1/
2      Ahmet/Erdem/Aydin         /2/2/
3      Ahmet/Erdem/Aydin/Tomrs   /2/2/2/

You don't have to use recursive CTE (Denis Valeev)'s Answer, it has low performance. Just join it with herself

DECLARE @hierarchy TABLE (name VARCHAR(20), [path] HIERARCHYID)
INSERT INTO @hierarchy (name, path)
VALUES  
 ('Ahmet', '/')
,('Aliye', '/1/')
,('Selen', '/1/1/')
,('Erdem', '/2/')
,('Bilge', '/2/1/')
,('Aydin', '/2/2/')
,('Tomrs', '/2/2/2/')

--SELECT * FROM @hierarchy AS h

SELECT
    path.GetLevel() As Level,
    (
        SELECT STRING_AGG(A.name, '/')
        FROM
            @hierarchy A
        WHERE
            B.path.IsDescendantOf(A.path) = 1               
    ) AS FullName,
    path.ToString() AS Path
FROM
    @hierarchy B

Output :

Level  Fullname                  Path
------ ------------------------- ----------
0      Ahmet                     /
1      Ahmet/Aliye               /1/
2      Ahmet/Aliye/Selen         /1/1/
1      Ahmet/Erdem               /2/
2      Ahmet/Erdem/Bilge         /2/1/
2      Ahmet/Erdem/Aydin         /2/2/
3      Ahmet/Erdem/Aydin/Tomrs   /2/2/2/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文