如何递归获取逗号分隔的 CategoryIds?
How to get comma delimited CategoryIds recursively?
我有一个 table 这样的:
Name CategoryId ParentCategoryId
Footwear 93 0
Men Shoes 6 93
Female Shoes 7 93
Mobile 2 0
Smartphone 4 2
我需要这样的输出:
Name Categories
Footwear 93,0
Men Shoes 6,93,0
Female Shoes 7,93,0
Mobile 2,0
Smartphone 4,2,0
基本上,我需要递归地获取类别 ID 并将它们变成逗号分隔的字符串。 3 年后我进入 SQL
,我不知道如何得到这个结果。我尝试了其他 SO 问题的解决方案,但仍然没有成功。
你用递归 cte 做这个:
DECLARE @t TABLE
(
Name VARCHAR(100) ,
CategoryId INT ,
ParentCategoryId INT
)
INSERT INTO @t
VALUES ( 'Footwear', 93, 0 ),
( 'Men Shoes', 6, 93 ),
( 'Female Shoes', 7, 93 ),
( 'Mobile', 2, 0 ),
( 'Smartphone', 4, 2 );
WITH cte
AS ( SELECT * ,
CAST(CategoryId AS VARCHAR(100)) AS Categories
FROM @t
WHERE ParentCategoryId = 0
UNION ALL
SELECT t.* ,
CAST(CAST(t.CategoryId AS VARCHAR(100)) + ','
+ c.Categories AS VARCHAR(100))
FROM @t t
JOIN cte c ON c.CategoryId = t.ParentCategoryId
)
SELECT *
FROM cte
用递归 CTE 试试:
DECLARE @tbl TABLE(Name VARCHAR(100),CategoryId INT,ParentCategoryId INT);
INSERT INTO @tbl VALUES
('Footwear',93,0)
,('Men Shoes',6,93)
,('Female Shoes',7,93)
,('Mobile',2,0)
,('Smartphone',4,2);
--based on this:
WITH tree (CategoryId, ParentCategoryId, level, Name, rn, IdList) as
(
SELECT CategoryId, ParentCategoryId, 0 as level, Name,
convert(varchar(max),right(row_number() over (order by CategoryId),10)) AS rn,
convert(varchar(max),ISNULL(CategoryId,0)) AS IdList
FROM @tbl
WHERE ParentCategoryId = 0
UNION ALL
SELECT c2.CategoryId, c2.ParentCategoryId, tree.level + 1, c2.Name,
rn + '/' + convert(varchar(max),right(row_number() over (order by tree.CategoryId),10)),
convert(varchar(max),c2.CategoryId) + ',' + IdList
FROM @tbl c2
INNER JOIN tree ON tree.CategoryId = c2.ParentCategoryId
)
SELECT *
FROM tree
order by RN
部分结果:
1 Mobile 2
1/1 Smartphone 4,2
2 Footwear 93
2/1 Men Shoes 6,93
2/2 Female Shoes 7,93
我有一个 table 这样的:
Name CategoryId ParentCategoryId
Footwear 93 0
Men Shoes 6 93
Female Shoes 7 93
Mobile 2 0
Smartphone 4 2
我需要这样的输出:
Name Categories
Footwear 93,0
Men Shoes 6,93,0
Female Shoes 7,93,0
Mobile 2,0
Smartphone 4,2,0
基本上,我需要递归地获取类别 ID 并将它们变成逗号分隔的字符串。 3 年后我进入 SQL
,我不知道如何得到这个结果。我尝试了其他 SO 问题的解决方案,但仍然没有成功。
你用递归 cte 做这个:
DECLARE @t TABLE
(
Name VARCHAR(100) ,
CategoryId INT ,
ParentCategoryId INT
)
INSERT INTO @t
VALUES ( 'Footwear', 93, 0 ),
( 'Men Shoes', 6, 93 ),
( 'Female Shoes', 7, 93 ),
( 'Mobile', 2, 0 ),
( 'Smartphone', 4, 2 );
WITH cte
AS ( SELECT * ,
CAST(CategoryId AS VARCHAR(100)) AS Categories
FROM @t
WHERE ParentCategoryId = 0
UNION ALL
SELECT t.* ,
CAST(CAST(t.CategoryId AS VARCHAR(100)) + ','
+ c.Categories AS VARCHAR(100))
FROM @t t
JOIN cte c ON c.CategoryId = t.ParentCategoryId
)
SELECT *
FROM cte
用递归 CTE 试试:
DECLARE @tbl TABLE(Name VARCHAR(100),CategoryId INT,ParentCategoryId INT);
INSERT INTO @tbl VALUES
('Footwear',93,0)
,('Men Shoes',6,93)
,('Female Shoes',7,93)
,('Mobile',2,0)
,('Smartphone',4,2);
--based on this:
WITH tree (CategoryId, ParentCategoryId, level, Name, rn, IdList) as
(
SELECT CategoryId, ParentCategoryId, 0 as level, Name,
convert(varchar(max),right(row_number() over (order by CategoryId),10)) AS rn,
convert(varchar(max),ISNULL(CategoryId,0)) AS IdList
FROM @tbl
WHERE ParentCategoryId = 0
UNION ALL
SELECT c2.CategoryId, c2.ParentCategoryId, tree.level + 1, c2.Name,
rn + '/' + convert(varchar(max),right(row_number() over (order by tree.CategoryId),10)),
convert(varchar(max),c2.CategoryId) + ',' + IdList
FROM @tbl c2
INNER JOIN tree ON tree.CategoryId = c2.ParentCategoryId
)
SELECT *
FROM tree
order by RN
部分结果:
1 Mobile 2
1/1 Smartphone 4,2
2 Footwear 93
2/1 Men Shoes 6,93
2/2 Female Shoes 7,93