Query syntax

Using MS SQL 2008.

I have a table (see below) that has the cumulative values for Diagnosed and Completed for each year.

I need to update the CumulativeActive column by subtracting the CumulativeCompleted from the previous year from the CumulativeDiagnosed of the current year.  

The table should look like this.  The one exception is the first year as then there is no previous year so you can do the subtraction.

yr      CumulativeDiagnosed   CumulativeActive   CumulativeCompleted
2010     4                                    0                                1
2011    16                                   0                                2
2012    27                                   0                                2
2013    40                                   0                                3
2014    121                                 0                                8

Open in new window


so the table should look like this:

yr      CumulativeDiagnosed   CumulativeActive   CumulativeCompleted
2010     4                                    4                               1
2011    16                                   15                             2
2012    27                                   25                             2
2013    40                                   38                             3
2014    121                                 118                           8

Open in new window



Can anyone help me with the script?

/thanks
soozhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
are those the only columns in that table ?
What is the primary key of that table ?
0
Scott PletcherSenior DBACommented:
UPDATE curr_yr
SET  CumulativeActive =  curr_yr.CumulativeDiagnosed -
    ISNULL(prev_yr.CumulativeCompleted, 0)
FROM table_name curr_yr
LEFT OUTER JOIN table_name prev_yr ON
    prev_yr.yr = curr_yr.yr - 1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
soozhAuthor Commented:
Thanks that is perfect.  I was struggeling with a solution that ordered the rows and did not Think i could just join on the current and previous year.  Also neat way to handle the first record with the isnull.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.