Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何递归获取逗号分隔的categoryId?_Sql_Sql Server_Tsql_Sql Server 2014 - Fatal编程技术网

Sql 如何递归获取逗号分隔的categoryId?

Sql 如何递归获取逗号分隔的categoryId?,sql,sql-server,tsql,sql-server-2014,Sql,Sql Server,Tsql,Sql Server 2014,我有一张这样的桌子: Name CategoryId ParentCategoryId Footwear 93 0 Men Shoes 6 93 Female Shoes 7 93 Mobile 2 0 Smartphone 4 2 我需要像这样的输出: Name Categori

我有一张这样的桌子:

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: http://stackoverflow.com/a/5522641/5089204
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

谢谢。这对我有用。您还可以告诉我如何从逗号分隔的字符串列中删除最后一个始终为0的Id吗?