• Subbu S (12/16/2012)


    Finally i found the solution, here u go,

    Select 1 [Id],1000[value] into TempA

    Select 2[Id], 10 value, 1 parent into TempB

    Insert into TempB

    Select 3,9,1

    union all

    select 4,5,1

    Select * From TempA;

    Select * From TempB;

    Select ROW_NUMBER() Over (Partition By Parent Order By B.Id) Rnum,B.*,A.value [PVal] into tempc From tempb B

    join tempa A on a.id = b.parent

    Select * From TempC;

    Select *, PVal - (Select SUM(value) From tempc E Where E.parent = D.Parent AND E.Rnum <= D.Rnum) From tempc D

    As you mentioned in your original post that you don't want to use Loop or Cursor.But in your posted query also the inner query will execute repeatedly for outer query.Is not it ?

    Then what is the gain you had.