SQL Server递归查询以显示父级路径

3

我正在处理SQL Server语句,有一个类似的表:

| item | value | parentItem |
+------+-------+------------+
|  1   | 2test |     2      |
|  2   | 3test |     3      |
|  3   | 4test |     4      |
|  5   | 1test |     1      |
|  6   | 3test |     3      |
|  7   | 2test |     2      |

我希望使用SQL Server语句获得以下结果:

| item1 | value1                   |
+-------+--------------------------+
|  1    | /4test/3test/2test       |
|  2    | /4test/3test             |
|  3    | /4test                   |
|  5    | /4test/3test/2test/1test |
|  6    | /4test/3test             |
|  7    | /4test/3test/2test       |

我还没有找到正确的SQL语句来获取所有parentItem对应的所有id的值。

我尝试过这个SQL语句:

with all_path as 
(
    select item, value, parentItem 
    from table 

    union all 

    select a.item, a.value, a.parentItem 
    from table a, all_path b
    where a.item = b.parentItem
)
select 
    item as item1, 
    stuff(select '/' + value 
          from all_path 
          order by item asc 
          for xml path ('')), 1, 0, '') as value1 
from 
    all_path

但是结果中的"value1"列如下所示:

/4test/4test/4test/3test/3test/3test/3test/2test/2test/2test/2test

你能帮我翻译一下吗?谢谢。


2
你只是想要这个作为输出吗?还是你想将表中的数据更改为不同的结构?我认为在任何情况下都不需要使用MERGE,你尝试过什么? - undefined
谢谢你的问题。我已经更新了问题。你能帮忙检查一下吗? - undefined
SQL Server 2008 R2 - undefined
1
好的,尤其是在使用古老且不再支持的版本时,最好提前标明,因为人们通常会期望更新的版本(其中解决方案可能会有所不同/更好)。 - undefined
嗨 @AnneLiu,干得漂亮!我的解决方案如下非常相似。我觉得你在最后的选择语句中缺少了 GROUP BY。 - undefined
2个回答

6

基于您提供的预期输出,使用递归部分将值连接起来。

;with yourTable as (
     select item, value, parentItem 
     from (values 
     (1,'2test',2)
    ,(2,'3test',3)
    ,(3,'4test',4)
    ,(5,'1test',1)
    ,(6,'3test',3)
    ,(7,'2test',2)
    )x (item,value,parentItem)
)
, DoRecursivePart as (

    select 1 as Pos, item, convert(varchar(max),value) value, parentItem 
    from yourTable
    union all
    select drp.pos +1, drp.item, convert(varchar(max), yt.value + '/' + drp.value), yt.parentItem
    from yourTable yt
    inner join DoRecursivePart drp on drp.parentItem = yt.item

)
select drp.item, '/' + drp.value 
from DoRecursivePart drp
inner join (select item, max(pos) mpos 
            from DoRecursivePart 
            group by item) [filter] on [filter].item = drp.item and [filter].mpos = drp.Pos
order by item

提供

item        value
----------- ------------------
1           /4test/3test/2test
2           /4test/3test
3           /4test
5           /4test/3test/2test/1test
6           /4test/3test
7           /4test/3test/2test

3

这是示例数据

drop table if exists dbo.test_table;
go
create table dbo.test_table(
  item                  int not null,
  [value]               varchar(100) not null,
  parentItem            int not null);

insert dbo.test_table values
(1,'test1',2),
(2,'test2',3),
(3,'test3',4),
(5,'test4',1),
(6,'test5',3),
(7,'test6',2);

以下是查询内容

;with recur_cte(item, [value], parentItem, h_level) as (
    select item, [value], parentItem, 1 
    from dbo.test_table tt
    union all
    select rc.item, tt.[value], tt.parentItem, rc.h_level+1 
    from dbo.test_table tt join recur_cte rc on tt.item=rc.parentItem)
select rc.item, 
      stuff((select '/' + cast(parentItem as varchar)
             from recur_cte c2
             where rc.item = c2.item
             order by h_level desc FOR XML PATH('')), 1, 1, '') [value1]
from recur_cte rc
group by item;

以下是结果

item    value1
1       4/3/2
2       4/3
3       4
5       4/3/2/1
6       4/3
7       4/3/2

2
string_agg在SQL Server 2008中不受支持。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接