[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


MS SQL 2012 and computing variances

Posted on 2014-07-16
Medium Priority
Last Modified: 2014-07-22
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.
Question by:JamesDBuskirk
LVL 13

Accepted Solution

magarity earned 2000 total points
ID: 40201103
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.

Author Closing Comment

ID: 40212037
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.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question