Link to home
Start Free TrialLog in
Avatar of Jeff S
Jeff SFlag for United States of America

asked on

SQL question - help with insert for missing info

I am having issues with a crystal report not displaying values, not because of the crystal report, rather the SQL is missing in my file to populate the report.

Essentially, what I need in my SQL are inserts to fill in the holes and looking for advice on best practice to do so.

In my report, I am outputting the following fields:

Patient Visit Charge
Patient Payment
Insurance Payment
Patient Adjustment
Insurance Adjustment

My output could have one of these or it may have all of these for a given month / Financial Class combination. If its missing any one of these, I need to insert the missing values and report specific values to it.

My report columns are as such:

Type , Amount , Action , Source , FinancialClass and Date

Table Structure of my temp table that's getting the records from my main query:

CREATE TABLE #Output
 (
        [Type]                 VARCHAR(5)
      , [Amount]            NUMERIC(10 , 2)
      , [Action]               CHAR(1)
      , [Source]              INT
      , [FinancialClass] VARCHAR(60)
      , [Date]                  DATETIME
 );

Type will always be PA or C. PA for Payment or Adjustment and C for Charge.
Amount will be the sum of the transactions for the month for the unique Type, Source and FinancialClass.
Action with be either P for Payment or A for Adjustment. It will be NULL on Charges.
Source is either 1 or 2. 1 for Patient and 2 for Insurance.
FinancialClass will be determined by client system and different. Example (Medicare, BlueCrossBlueShield, Aetna, Cigna) ...
Date will be  month date for the transactions formatted as such ( 2013-02-01 00:00:00.000 )

Sample output:

PA      25.50      P              1              Sliding Scale      2013-02-01 00:00:00.000
PA      2308.00      P              2              Medicare      2013-02-01 00:00:00.000
C      130.00      NULL      NULL      Commercial      2013-02-01 00:00:00.000
PA      1500.00      P              2              HMO              2013-02-01 00:00:00.000
C      130.00      NULL      NULL      HMO              2013-02-01 00:00:00.000

With the sample output, I would need to do the following inserts; again needing all 5 of the values to prevent holes in my data:

C     0              NULL       NULL      Sliding Scale  2013-02-01 00:00:00.000
PA   0              P              2             Sliding Scale   2013-02-01 00:00:00.000
PA      25.50      P              1              Sliding Scale      2013-02-01 00:00:00.000
PA   0              A              2             Sliding Scale   2013-02-01 00:00:00.000
PA      0            A              1              Sliding Scale      2013-02-01 00:00:00.000
C     0              NULL       NULL      Medicare        2013-02-01 00:00:00.000
PA      2308.00      P              2              Medicare      2013-02-01 00:00:00.000
PA      0              P              2              Medicare      2013-02-01 00:00:00.000
PA   0              A              2             Medicare        2013-02-01 00:00:00.000
PA      0            A              1              Medicare      2013-02-01 00:00:00.000
C      130.00      NULL      NULL      Commercial      2013-02-01 00:00:00.000
PA   0              P              2             Commercial   2013-02-01 00:00:00.000
PA      0              P              1              Commercial      2013-02-01 00:00:00.000
PA   0              A              2             Commercial   2013-02-01 00:00:00.000
PA      0            A              1              Commercial      2013-02-01 00:00:00.000
C      130.00      NULL      NULL      HMO              2013-02-01 00:00:00.000
PA      1500.00      P              2              HMO              2013-02-01 00:00:00.000
PA      0              P              1              HMO              2013-02-01 00:00:00.000
PA   0              A              2             HMO               2013-02-01 00:00:00.000
PA      0            A              1              HMO             2013-02-01 00:00:00.000

In my output, I need a record for all five transaction combinations (Charge, Ins Payment, Patient Payment, Ins Adjustment and Patient Adjustment) for the unique Financial Class and Dates. Again, 4 things could be missing or nothing could be missing, but if something is, I need to insert the missing values.

I hope I explained this well, if not, let me know. Thank you immensely in advance for any suggestions/help with the code for this.
Avatar of HainKurt
HainKurt
Flag of Canada image

can you please try http://sqlfiddle.com

and setup your table here...
SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeff S

ASKER

I read the responses and it was Pauls comment about looking at the other query that engaged my mind - I took the existing and re-formatted it to do all my values differently. At the end of the day, I got my numbers I needed and info back as I desired. I will split points for those that took the time to look and attempt to help and offer suggestions and insight. Thanks again!!
Avatar of Jeff S

ASKER

Thank you for looking!