MS SQL 2012 and computing variances

Posted on 2014-07-16
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

    Yes, something involved with "OVER" is the answer. It is called the RANK function.  Here is the MSDN page describing how it works:
    In your case, you will partition by the DDateYYQ field.

    Author Closing Comment

    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

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now