SQL Server CTE 递归 - 将树追溯到每个节点的特定树级别
我有两个表,Item(ItemID、CatID、Name)和Category(CatID、ParentID、Name、CatLevel)。 类别是项目所属类别的树形层次结构。该表还跟踪每个节点所在的类别级别。下面是一个示例:
1. Animal (CatLevel = 1)
2. Dog (CatLevel = 2)
3. Beagle (CatLevel = 3)
4. Labrador (CatLevel = 3)
5. Yellow (CatLevel = 4)
6. Chocolate (CatLevel = 4)
7. Black (CatLevel = 4)
8. Cat (CatLevel = 2)
9. Tabby (CatLevel = 3)
10. Horse (CatLevel = 2)
11. Stallion (CatLevel = 3)
我正在尝试提出一个查询,该查询为我提供了具有常规 CatID 和我指定级别(例如级别 2)的 CatID 的项目列表。示例输出:
Item CatID LevelTwoCatID
______ _____ _____________
Dog 2 2
Beagle 3 2
Labrador 4 2
Yellow 5 2
Chocolate 6 2
Black 7 2
Cat 8 8
Tabby 9 8
Horse 10 10
Stallion 11 10
我知道如何追溯到单个节点的类别,但我不知道如何进行更通用的查询来为每个节点提供“LevelTwoCatID”。以下是我的做法:
WITH Tree AS
(
SELECT * FROM Category WHERE CatID = 6 -- Chocolate lab, change for some other
UNION ALL
SELECT * FROM Category AS a
INNER JOIN Tree t ON a.CatID = t.ParentID
)
SELECT * FROM Tree WHERE CatLevel = 2
如何获取所有节点/叶子的 CatLevel = 2 节点 ID?
谢谢!
I have two tables, Item (ItemID, CatID, Name) and Category (CatID, ParentID, Name, CatLevel). Category is a tree hierarchy of categories that items can be in. The table also tracks what category level each node is in. Here's an example:
1. Animal (CatLevel = 1)
2. Dog (CatLevel = 2)
3. Beagle (CatLevel = 3)
4. Labrador (CatLevel = 3)
5. Yellow (CatLevel = 4)
6. Chocolate (CatLevel = 4)
7. Black (CatLevel = 4)
8. Cat (CatLevel = 2)
9. Tabby (CatLevel = 3)
10. Horse (CatLevel = 2)
11. Stallion (CatLevel = 3)
I'm trying to come up with a query that gives me a list of items with their regular CatID and a CatID at a level I specify (for example, level 2). Example output:
Item CatID LevelTwoCatID
______ _____ _____________
Dog 2 2
Beagle 3 2
Labrador 4 2
Yellow 5 2
Chocolate 6 2
Black 7 2
Cat 8 8
Tabby 9 8
Horse 10 10
Stallion 11 10
I know how to trace back up to the category of a single node, but I don't know how to make a more generic query that will provide the "LevelTwoCatID" for every node. Here's how I'd do it for one:
WITH Tree AS
(
SELECT * FROM Category WHERE CatID = 6 -- Chocolate lab, change for some other
UNION ALL
SELECT * FROM Category AS a
INNER JOIN Tree t ON a.CatID = t.ParentID
)
SELECT * FROM Tree WHERE CatLevel = 2
How can I get the CatLevel = 2 node ID for all nodes/leaves?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这篇文章已经很老了,但我是这样做的。
在我的表中有几个根类别,因此我使用 @RootId 来指定我要查找哪一个。根类别的 ParentID 设置为 NULL。
如果您的类别表中没有 CatLevel 列 - cte Tree 会为您创建它。如果要经常使用查询,最好将其添加到表中(UPDATE代码在最后注释)。
如果您希望拥有动态列数(不像我的情况那样固定为 5),请为其创建动态 SQL 查询。
这是查询前后的屏幕截图
I know the post is quite old, but here is how I did that.
In my table there are several root categories, therefore I use @RootId to specify which one I am after. Root category has ParentID set to NULL.
If you do not have CatLevel column in your categories table - cte Tree creates it for you. If you are going to use the query frequently, it is better to add it to the table (UPDATE code is commented in the end).
If you wish to have dynamic number of columns (not fixed to 5 as in my case), make a dynamic SQL query for it.
Here's screenshot of the before and after queries