SQL Concat递归值?

问题描述 投票:0回答:1

我正在尝试(递归地)获取类别中的零件编号的完整列表(再次-递归)。多维递归?

想象类别表很简单:

_____________________________________________
|   id  |    name    |   parent_category_id |
---------------------------------------------
|   1   |     Foo    |         NULL         |
|   2   |     Bar    |         NULL         |
|   3   |   FooBaby  |           1          |
|   4   |   BarBaby  |           2          |
|   5   |   FoGBaby  |           3          |
|   6   |   F2GBaby  |           3          |
|   7   |   BaGBaby  |           4          |
|   8   |   B2GBaby  |           4          |
_____________________________________________

part_categories表是这样的:

_________________________________________________
|   id   |       part_number    |  category_id  |
-------------------------------------------------
|    1   |          0000        |       8       |
|    2   |          0001        |       8       |
|    3   |          0002        |       8       |
|    4   |          0003        |       7       |
|    5   |          0004        |       7       |
|    6   |          0005        |       7       |
|    7   |          0006        |       6       |
|    8   |          0007        |       5       |
|    9   |          0008        |       4       |
|   10   |          0009        |       3       |
|   11   |          0010        |       2       |
|   12   |          0011        |       1       |
_________________________________________________

我可以很容易地建立类别表的递归(想想y轴):

WITH sub_tree
AS (
  SELECT categories.id, categories.name, categories.parent_category_id, 1 AS depth
  FROM categories
  WHERE parent_category_id IS NULL

  UNION ALL

  SELECT subcategories.id, subcategories.name, subcategories.parent_category_id, sub_tree.depth + 1
  FROM categories as subcategories, sub_tree
  WHERE subcategories.parent_category_id = sub_tree.id
)
SELECT * FROM sub_tree;

我得到这样的结果:

_______________________________________________________
|   id  |    name    |   parent_category_id |  depth  |
-------------------------------------------------------
|   1   |     Foo    |         NULL         |     1   |  
|   2   |     Bar    |         NULL         |     1   |   
|   3   |   FooBaby  |           1          |     2   |  
|   4   |   BarBaby  |           2          |     2   |  
|   5   |   FoGBaby  |           3          |     3   |  
|   6   |   F2GBaby  |           3          |     3   |  
|   7   |   BaGBaby  |           4          |     3   |  
|   8   |   B2GBaby  |           4          |     3   |  
_______________________________________________________

我想要这样的结果,在整个递归类别中具有唯一的部件号:

_______________________________________________________________________
|   id  |    name    |   parent_category_id |       part_numbers      |
-----------------------------------------------------------------------
|   1   |     Foo    |         NULL         | 0011,0009,0006,0007...  |
|   2   |     Bar    |         NULL         | 0010,0008,0003,0004...  |
|   3   |   FooBaby  |           1          |     0009,0007,0006      |
|   4   |   BarBaby  |           2          | 0008,0005,0004,0003...  |
|   5   |   FoGBaby  |           3          |          0007           |
|   6   |   F2GBaby  |           3          |          0006           |
|   7   |   BaGBaby  |           4          |      0003,0004,0005     |
|   8   |   B2GBaby  |           4          |      0000,0001,0002     |
_______________________________________________________________________

任何想法我如何实现这一目标?

sql-server recursion union union-all
1个回答
0
投票

您应该尝试一下,我认为这与您所寻找的相同,

;WITH sub_tree
AS (
 SELECT categories.id, categories.name, categories.parent_category_id, 1 AS depth
 FROM #categories as categories
 WHERE parent_category_id IS NULL

 UNION ALL

 SELECT subcategories.id, subcategories.name, subcategories.parent_category_id, 
sub_tree.depth + 1
 FROM #categories as subcategories, sub_tree
 WHERE subcategories.parent_category_id = sub_tree.id
)

SELECT * ,
  STUFF(
   (SELECT ',' + CONVERT(VARCHAR(MAX),s.part_number)
  FROM #part_categories  s
  WHERE s.category_id = sub_tree.id
 FOR XML PATH('')),1,1,'') AS part_numbers
 FROM sub_tree;

我刚刚扩展了您现有的查询,以便您轻松理解。

_______________________________________________________________________
| id    | name      | parent_category_id    |   depth   | part_numbers
| 1     | Foo       | NULL                  |   1       | 0011
| 2     | Bar       | NULL                  |   1       | 0010
| 4     | BarBaby   | 2                     |   2       | 0008
| 7     | BaGBaby   | 4                     |   3       | 0003,0004,0005
| 8     | B2GBaby   | 4                     |   3       | 0000,0001,0002
| 3     | FooBaby   | 1                     |   2       | 0009
| 5     | FoGBaby   | 3                     |   3       | 0007
| 6     | F2GBaby   | 3                     |   3       | 0006

© www.soinside.com 2019 - 2024. All rights reserved.