简体   繁体   English

SQL中树形结构数据的分层更新查询

[英]Hierarchical update query for the tree structure data in SQL

create table #tmp(ChildID nvarchar(50),ParentID nvarchar(50), Percentage numeric(8,2))
insert into #tmp values ('1', NULL,NULL)
insert into #tmp values ('1.1', '1',89)
insert into #tmp values ('1.2', '1',NULL)
insert into #tmp values ('1.2.1','1.2',96)
insert into #tmp values ('1.2.2', '1.2',NULL)
insert into #tmp values ('1.2.2.1', '1.2.2',87)
insert into #tmp values ('1.2.2.2', '1.2.2',NULL)
insert into #tmp values ('1.2.2.2.1', '1.2.2.2',87)

在此处输入图片说明

I am trying CTE update but couldn't attack the expected result yet. 我正在尝试CTE更新,但无法攻击预期的结果。 Can some one help in this. 可以帮上忙。

I think this is what you want: 我认为这是您想要的:

With cte as
(Select ChildId, Sum(Percentage) as [percent] from (
Select t1.ChildID,t1.Percentage as oldPercent, t2.ParentID, t2.Percentage from #tmp t1 
join #tmp t2 on t2.ParentID like t1.ChildID +'%')c 
group by ChildId
)
Update #tmp 
set Percentage = cte.[percent]
from #tmp join cte
on #tmp.ChildID = cte.ChildID

Have a look at this. 看看这个。

DECLARE @tmp TABLE  (ChildID NVARCHAR(50)
                   ,ParentID NVARCHAR(50)
                   ,Percentage NUMERIC(8, 2)
                   )
INSERT  INTO @tmp   VALUES  ('1', NULL, NULL) 
INSERT  INTO @tmp   VALUES  ('1.1', '1', 89)
INSERT  INTO @tmp   VALUES  ('1.2', '1', NULL)
INSERT  INTO @tmp   VALUES  ('1.2.1', '1.2', 96)
INSERT  INTO @tmp   VALUES  ('1.2.2', '1.2', NULL)
INSERT  INTO @tmp   VALUES  ('1.2.2.1', '1.2.2', 57)
INSERT  INTO @tmp   VALUES  ('1.2.2.2', '1.2.2', NULL)
INSERT  INTO @tmp   VALUES  ('1.2.2.2.1', '1.2.2.2', 62);

WITH    cteTree
          AS (SELECT    t1.ChildID, t1.childid AS OriginalID
              ,         t1.Percentage
              FROM      @tmp t1
              UNION ALL
              SELECT t2.ChildID
              ,      c.OriginalID
              ,      t2.Percentage
              FROM      @tmp t2
              JOIN cteTree c ON t2.parentID = c.ChildID
             )

SELECT OriginalID AS ChildID, SUM(cteTree.Percentage)
FROM cteTree
GROUP BY OriginalID
ORDER BY cteTree.OriginalID

The other guys were pretty close, but needs to iterate slightly differently, and iteration can be tricky if you don't use it a lot. 其他人都非常接近,但是需要略有不同的迭代,如果您不经常使用迭代,迭代可能会很棘手。

You can try this : 您可以尝试以下方法:

DECLARE  @temp table(ChildID nvarchar(50),ParentID nvarchar(50), Percentage numeric(8,2))
insert into @temp values ('1', NULL,NULL)
insert into @temp values ('1.1', '1',89)
insert into @temp values ('1.2', '1',NULL)
insert into @temp values ('1.2.1','1.2',96)
insert into @temp values ('1.2.2', '1.2',NULL)
insert into @temp values ('1.2.2.1', '1.2.2',57)
insert into @temp values ('1.2.2.2', '1.2.2',NULL)
insert into @temp values ('1.2.2.2.1', '1.2.2.2',62)

;With cte
AS
(
select  * ,ROW_NUMBER()Over(Order by (select NULL))Rn  from @temp
)
Select 
ChildID
,ParentID
,Case when Percentage IS NULL Then (select SUM(c.Percentage) From Cte c Where c.Rn>cte.Rn) Else Percentage END Percentage
from cte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM