• I'm not sure if you're expecting something like this.

    CREATE TABLE PSITableFinal

    (DivisionRegion char(1),

    PSIYear char(4),

    PSIMonth char(2),

    PSIKey int,

    PSIOutcome int)

    INSERT INTO PSITableFinal VALUES

    ('A','2014','01',2,1),

    ('A','2014','02',2,0),

    ('A','2014','03',2,0),

    ('A','2014','04',2,0),

    ('A','2014','05',2,0),

    ('A','2014','06',2,0),

    ('A','2014','07',2,0)

    DECLARE @Month char(2) = '07',

    @Year char(4) = '2014'

    SELECT COUNT(*)

    FROM [PSITableFinal] f

    WHERE PSIYear = @Year

    AND PSIMonth <= @Month

    AND PSIOutcome = 0

    AND PSIMonth >= ( SELECT MAX(x.PSIMonth)

    FROM [PSITableFinal] x

    WHERE x.PSIYear = @Year

    AND x.PSIMonth <= @Month

    AND x.PSIOutcome > 0)

    --Change the value for July to test both cases.

    UPDATE [PSITableFinal] SET

    PSIOutcome = 1

    WHERE PSIYear = @Year

    AND PSIMonth = @Month

    SELECT COUNT(*)

    FROM [PSITableFinal] f

    WHERE PSIYear = @Year

    AND PSIMonth <= @Month

    AND PSIOutcome = 0

    AND PSIMonth >= ( SELECT MAX(x.PSIMonth)

    FROM [PSITableFinal] x

    WHERE x.PSIYear = @Year

    AND x.PSIMonth <= @Month

    AND x.PSIOutcome > 0)

    GO

    DROP TABLE [PSITableFinal]

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2