I need to sum points on each level earned by a tree of users. Level 1 is the sum of users\' points of the users 1 level below the user. Level 2 is the Level 1 points of the
You can write a simple recursive function to do the job. My MSSQL is a little bit rusty, but it would look like this:
CREATE FUNCTION CALC
(
@node integer,
)
returns
(
@total integer
)
as
begin
select @total = (select node_value from yourtable where node_id = @node);
declare @children table (value integer);
insert into @children
select calc(node_id) from yourtable where parent_id = @node;
@current = @current + select sum(value) from @children;
return
end