MS SQL 2012 and computing variances

I am using MS SQL 2012. I have a need to calculate some seemingly simple variances and need assistance. I have a single table with patient data records, and the three fields needed for this example are 'Provider', 'DDateYYQ' and 'Charges'. They are of type NCHAR, Integer, and Money respectively. I regularly create a summary by Provider for each 'DDateYYQ' with Case Counts (sum of total records) and Charges, as demonstrated below.

Provider Summary Report for 'DDateYYQ'= 142 (i.e. 2014 Quarter 2)
Provider   Cases    Charges
-------   -----     -------
aaaaa     4,285     $1,425,000.00
bbbbb     6,221     $  483,000.00
...
...
nnnnn     2,231   $ 1,221,224.66

I need to analyze variances in the case counts and charges ACROSS the DDateYYQ field, as shown below for two quarters of data. It does not need the exact format I am showing below, but I desperately need to see these variances. There is a fourth field involved 'Payer" and I will include only a WHERE statement for this field (I am analyzing Unknown Payers which have a value of '99').

Variance Report for Payer='99'
                      142              142               141              141              141-142           141-142
Provider   Cases          Charges    Cases          Charges       Case Variance     Charge Variance
-------   -----     -------------    -----     -------------      -------------     -------------
aaaaa     4,285     $1,425,000.00    4,081     $1,475,000.00          delta            delta
bbbbb     6,221     $  483,000.00    6,5234        83,500.00          delta            delta
...
...
nnnnn     2,231    $ 1,221,224.66    2,422     $1,223,443.00          delta            delta

Ultimately I need to expand the date range and analyze years of data at a time, and perhaps up to a five year trend.

I obviously can calculate the difference between two fields, by I cannot wrap my mind around how to do the variances ACROSS the DDateYYQ field. Grouping doesn't seem to do it, I seem to remember from my classes an OVER command that may work here but the MSDN page on this didn't stir up much. Could someone help or point me in the right direction? I believe I can take the individual quarters and accomplish this in Excel, but am an Excel Novice.
LVL 1
JamesDBuskirkAsked:
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.

magarityCommented:
Yes, something involved with "OVER" is the answer. It is called the RANK function.  Here is the MSDN page describing how it works: http://msdn.microsoft.com/en-us/library/ms176102.aspx
In your case, you will partition by the DDateYYQ field.
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
JamesDBuskirkAuthor Commented:
Thank you for the feedback, I have read this article but have not had the time to try it yet. It would seem to be what I am looking for.
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 2008

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.