• Thank you so much for your response. I am sorry for changing the code.

    I cannot combine two update statements, since I need the first update value to calculate the next one.

    Here is the code and the logic which I am using.

    CREATE TABLE [dbo].[FunctionTST1](

    [id_task] [int] NULL,

    [id_fathertask] [int] NULL,

    [level_task] [int] NULL,

    [duracion_task] [float] NULL,

    [percent_task] [float] NULL,

    [name_task] [nvarchar](50) NULL

    );

    insert into [dbo].[FunctionTST1]

    values

    (1,1,1,76,NULL,'Task 1'),

    (2,1,2,60,NULL,'Task,1.1'),

    (3,2,3,2,100,'Task,1.1.1'),

    (4,2,3,38,NULL,'Task,1.1.2'),

    (5,4,4,10,100,'Task,1.1.2.1'),

    (6,4,4,22,20,'Task,1.1.2.2'),

    (7,4,4,5,35,'Task,1.1.2.3'),

    (8,4,4,1,40,'Task,1.1.2.4'),

    (9,2,3,20,NULL,'Task,1.1.3'),

    (10,9,4,20,20,'Task,1.1.3.1'),

    (11,1,2,9,NULL,'Task,1.2'),

    (12,11,3,2,0,'Task,1.2.1'),

    (13,11,3,7,NULL,'Task,1.2.2'),

    (14,13,4,2,50,'Task,1.2.2.1'),

    (15,13,4,5,50,'Task,1.2.2.2'),

    (16,1,2,10,0,'Task,1.3'),

    (17,1,2,20,0,'Task,1.4'),

    (18,1,2,6,NULL,'Task,1.5'),

    (19,18,3,6,NULL,'Task,1.5.1'),

    (20,19,4,6,NULL,'Task,1.5.1.1'),

    (21,20,5,6,10,'Task,1.5.1.1.1');

    The actual result is 25.37 and if convert into whole numbers during all the calclulations then it is 25.

    The aim of this is to find the percent_task for task1. Table is kind of a tree with Task1 as the root. Task one has 5 children Task 1.1, Task 1.2, Task 1.3, Task 1.4 and Task 1.5. Task 1.5 has one child Task 1.5.1 and it has one child Task 1.5.1.1. And Task 1.5.1.1 has one child Task 1.5.1.1.1 and so on.

    Task 1.1.2 has four children Task 1.1.2.1, Task 1.1.2.2, Task 1.1.2.3 and Task 1.1.2.4. and so on in the table.

    We need to start calculation from the leaf nodes. and result of the calulation replaces the percent_task of its parent.

    For example to calculate percent_task for Task 1.1.2, we need to use the value of his children.

    10*100 = 1000

    22*20 = 440

    5*35 = 175

    1*40 = 40

    -----------

    381655

    1655/38 = 43.55(44) = percent_task of task 1.1.2

    Hence the values of Task 1.1 = 38, Task 1.2 = 38.8, Task 1.3 = 0, Task 1.4 = 0 and Task 1.5 = 10 and final value is 25

    Hope the above one is understandable. I do not have much exp in this. Hence I developed the code from whatever you pasted and googling.