递归 CTE:父子关系

问题描述

有没有一种方法可以让我使用递归函数而不去到树的末尾? 例如:

假设我有一个像下面这样的简单树

对于每个节点,我想查看当前节点下已经计算的分数,并在其当前权重中使用它

Node score = (Current Kr)* (Current Weight) + (Next Node Already Computed score) *(Next Node Weight)
              -------------------------------------------------------------------------------------
                                        CurrentWeight + Next Node Weight

enter image description here

代码

CREATE TABLE TreeTable(
   Teamid           INTEGER  NOT NULL PRIMARY KEY,ParentId         INTEGER,Name             VARCHAR(255) NOT NULL,ParentName       VARCHAR(255),TeamRollupType             VARCHAR(17) NOT NULL,KR INTEGER  NOT NULL,TeamWeight INTEGER NOT NULL
);
INSERT INTO TreeTable(Teamid,ParentId,Name,ParentName,TeamRollupType,KR,TeamWeight) VALUES (1,NULL,'Team1','Current and Child',70,8);
INSERT INTO TreeTable(Teamid,TeamWeight) VALUES (2,1,'Team1A',98,1);
INSERT INTO TreeTable(Teamid,TeamWeight) VALUES (3,2,'Team1B',26,5);


select * from TreeTable;

树表结果:

enter image description here

尝试:我正在做的代码一直持续到最后一个节点

 with rcte as
(
  select 
         t.TeamID as GroupID,t.Name as GroupName,t.TeamRollupType as GroupType,t.TeamId,t.Name,t.TeamRollupType,--Value
         case t.TeamRollupType
           when 'Child List' then 0 
           when 'ChildAndLinkedLists' then 0 -- if the current node must be ignored,then the current value changes to 0
           else t.KR
         end as Value,--Weight
         case t.TeamRollupType
           when 'Child List' then 0 
           when 'ChildAndLinkedLists' then 0-- if the current node must be ignored,then the current value changes to 0
           else t.TeamWeight
         end as WeightValue,--Value*Weight
         Weightedscore=
         case t.TeamRollupType
           when 'Child List' then 0 
           when 'ChildAndLinkedLists' then 0-- if the current node must be ignored,then the current value changes to 0
           else t.KR
         end 
         *
         case t.TeamRollupType
           when 'Child List' then 0
           when 'ChildAndLinkedLists' then 0-- if the current node must be ignored,then the current value changes to 0
           else t.TeamWeight
         end 
        

  from  treetable t
union all
  select 
         r.GroupID,r.GroupName,r.GroupType,--TeamRollupType
         case r.TeamRollupType
           when 'Current List' then 'Current List' -- if the parent was the end of the sum,then it stays the end of the sum
           else t.TeamRollupType
         end,--Value
         case
           when r.TeamRollupType = 'Current List' then 0 -- if the parent was the end of the sum,then the current value changes to 0
           when t.TeamRollupType = 'Child List' then 0 
           when t.TeamRollupType ='ChildAndLinkedLists' then 0-- if the current node must be ignored,then the current value changes to 0
           else t.KR
         end,--TeamWeight
         case
           when r.TeamRollupType = 'Current List' then 0 -- if the parent was the end of the sum,then the current value changes to 0
           when t.TeamRollupType = 'Child List' then 0 -- if the current node must be ignored,then the current value changes to 0
           when t.TeamRollupType ='ChildAndLinkedLists' then 0
           else t.TeamWeight
         end,--Teamweight*Value
         case
           when r.TeamRollupType = 'Current List' then 0 -- if the parent was the end of the sum,then the current value changes to 0
           when t.TeamRollupType ='ChildAndLinkedLists' then 0
           else t.KR

         end 
         *
         case
           when r.TeamRollupType = 'Current List' then 0 -- if the parent was the end of the sum,then the current value changes to 0
           when t.TeamRollupType ='ChildAndLinkedLists' then 0
           else t.TeamWeight
         end

  from rcte r
  join treetable t
    on t.ParentId = r.TeamId 
)
select r.GroupName,sum(r.weightedscore)/SUM(CASE When r.Value <> 0 Then r.WeightValue Else 0 End ) as Groupscore,string_agg(case when r.Value <> 0 then convert(nvarchar(255),r.Value) end,'+') as GroupSumFormula,r.Name) end,'+') as GroupSumTeam,r.WeightValue) end,'+') as GroupSumWeight
       --string_agg(case when r.Value <> 0 then convert(nvarchar(255),r.weightedscore) end,'+') as GroupSumrweightedscore,--   sum(r.Value) as GroupSum,--SUM(CASE When r.Value <> 0 Then r.WeightValue Else 0 End ) as TotalWeight,--sum(r.weightedscore) as GroupWeightedSum
       

from rcte r
group by r.GroupName
order by r.GroupName;

结果:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)