you don't want to sum the value of the root ?declare @sample table( id int, pid int, value int)insert into @sampleselect 1, NULL, 10 union allselect 2, NULL, 20 union allselect 3, 1, 30 union allselect 4, 2, 40 union allselect 5, 3, 50 union allselect 6, 4, 60; with cte as( -- anchor select id, root = id, value = 0 from @sample s where pid is null union all select s.id, root = c.root, s.value from @sample s inner join cte c on s.pid = c.id)select root, sum(value)from ctegroup by root
KH[spoiler]Time is always against us[/spoiler]