• i have changed the my input and output and add date and comments to make it clear, for multiple entry with in a month it shall take the last entry.

    declare @t table

    ([id] int,[Amount] int,Sector char(1),Currentdate datetime);

    INSERT INTO @t

    ([id],[Amount],Sector,currentdate )

    VALUES

    (1, 100,'A','2002-01-01 00:00:00.000'),

    (2,50,'A','2002-01-02 00:00:00.000'),

    (3, 200,'B','2002-01-01 00:00:00.000'),

    (4, 300,'C','2002-01-01 00:00:00.000'),

    (5, 400,'A','2002-02-02 00:00:00.000'),

    (6, 500,'B','2002-02-02 00:00:00.000'),

    (7, 600,'C','2002-02-02 00:00:00.000'),

    (8, 100,'A','2002-03-02 00:00:00.000'),

    (9, 500,'B','2002-03-03 00:00:00.000'),

    (10, 100,'C','2002-03-03 00:00:00.000'),

    (11, 200,'C','2002-03-03 00:00:00.000')

    select * from @t

    declare @output table ([AfterIncreaseDecreaseAmount] int,Amount int,[Monthno]int,sector char(1))

    INSERT INTO @output

    ([AfterIncreaseDecreaseAmount],Amount,[Monthno],sector )

    VALUES

    (50,100,1,'A'), -- for 01/01/2002 it is 100 , for 02/02/2002 for multiple entry with in the month it shall take the last amount based on max(id) or last(currentdate).

    (+350,400,2,'A'), -- for second month 02/02/2002 it 400 > 50 it shows increase from first month so 400-50=+350 so --> AfterIncreaseDecreaseAmount=350

    (-300,100,3,'A'), -- for third month 02/03/2002 it is 100 < 400 (sum of 1st,2st month amount) so it shows decrease 100-400=-300 so-->AfterIncreaseDecreaseAmount=-300

    (+200,200,1,'B'),-- for b we have 200 it is 200 > 0 ,if it is only one it shall count that as 0 so it shows increase 200-0=200 --> AfterIncreaseDecrease=200

    (+300,500,2,'B'),-- for b we have 500 500 > 200--> 500-200=300 it show increased -->AfterIncreaseDecrease=300

    (0,500,3,'B'), -- for b in third month 500-500(sum of 1st,2stmonth amount of b) =0 AfterIncreaseDecrease=0

    (+300,300,1,'C'),

    (+300,600,2,'C'),

    (400,100,3,'C')

    select * from @output