如何解决递归 CTE:父子关系
有没有一种方法可以让我使用递归函数而不去到树的末尾? 例如:
对于每个节点,我想查看当前节点下已经计算的分数,并在其当前权重中使用它
Node score = (Current Kr)* (Current Weight) + (Next Node Already Computed score) *(Next Node Weight)
-------------------------------------------------------------------------------------
CurrentWeight + Next Node Weight
代码:
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;
树表结果:
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;
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。