Solved

SQL query

Posted on 2016-07-25
9
67 Views
Last Modified: 2016-08-22
declare @bcbs_load_date char(8)
INSERT INTO tbl_claims_bcbs_pharmacy_test
(MASTER_CONSUMER_ID,
CLAIM_NUMBER,
CLAIM_ADJUSTMENT_NUMBER,
CLAIM_DISPOSITION_CODE,
FILLED_DATE,
NDC_NUMBER,
DEA_NUMBER,
PHARMACY_NUMBER,
PHARMACY_NAME,
THERAPEUTIC_CLASS,
QUANTITY_DISPENSED,
DAYS_SUPPLIED,
FORMULARY_INDICATOR,
RETAIL_MAIL_INDICATOR,
BILLED_CHARGES,
[YEAR],
QUARTER,
[MONTH],
PATIENT_RESPONSIBILITY_AMOUNT,
PAID_AMOUNT,
SCRIPT_WRITTEN_DATE,
ALLOWED_AMT,
RX_NUMBER,
INGREDIENT_COST,
COPAY,
DRUG_NAME,
DISPENSED_BRAND_GENERIC_CODE,
GENERIC_PRODUCT_IDENTIFIER,
OTHER_PARTY_LIABILITY_AMOUNT,
PAID_DATE,
CLAIM_SOURCE_SYSTEM,
COINSRN_AMT,
DDCTBL_AMT,
MBR_SNCTNS_PNLTYS_AMT,
PAT_PAID_DFRNTL_AMT,
DISPENSE_AS_WRITTEN,
DISPENSE_DAYS_COUNT,
DRUG_STRENGTH,
MANUFACTURER_NAME,
INCURRED_YEAR_MONTH,
CLAIM_MAX_REVISION_DATE,
LAST_CLAIM_ADJUSTMENT,
PRIMARY_CARRIER_RESPONSIBLE_FOR_CLAIM,
ADJUDICATION_DATE,
CLAIM_PROCESSING_DATE,
CLAIM_ACTION_TYPE,
NPI,
PATIENT_ACCOUNT_NUMBER,
STATUS_CODE,
month_ending
)
SELECT  MASTER_CONSUMER_ID ,
        CLAIM_NUMBER ,
        CLAIM_ADJUSTMENT_NUMBER ,
        CLAIM_DISPOSITION_CODE ,
        FILLED_DATE ,
        NDC_NUMBER ,
        DEA_NUMBER ,
        PHARMACY_NUMBER ,
        PHARMACY_NAME ,
        THERAPEUTIC_CLASS ,
        SUM(cast(isnull(QUANTITY_DISPENSED ,'0.00') as float)) as [QUANTITY_DISPENSED],
        SUM(cast(isnull(DAYS_SUPPLIED ,'0') as float)) as [DAYS_SUPPLIED],
        FORMULARY_INDICATOR ,
        RETAIL_MAIL_INDICATOR ,
        SUM(cast(isnull(BILLED_CHARGES, '0.00') as money)) as [BILLED_CHARGES] ,
        [YEAR] ,
        QUARTER ,
        [MONTH] ,
        SUM(cast(isnull(PATIENT_RESPONSIBILITY_AMOUNT, '0.00') as money)) as [PATIENT_RESPONSIBILITY_AMOUNT] ,
        SUM(cast(isnull(PAID_AMOUNT, '0.00') as money)) as [PAID_AMOUNT] ,
        SCRIPT_WRITTEN_DATE ,
        SUM(cast(isnull(ALLOWED_AMT, '0.00') as money)) as [ALLOWED_AMT] ,
        RX_NUMBER ,
        sum(cast(isnull(INGREDIENT_COST,'0.00') as money)) as [INGREDIENT_COST],
        sum(cast(isnull(COPAY,'0.00') as money)) as [COPAY],
        DRUG_NAME ,
        DISPENSED_BRAND_GENERIC_CODE ,
        GENERIC_PRODUCT_IDENTIFIER ,
        sum(cast(isnull(OTHER_PARTY_LIABILITY_AMOUNT ,'0.00')as money)) as [OTHER_PARTY_LIABILITY_AMOUNT],
        PAID_DATE ,
        CLAIM_SOURCE_SYSTEM ,
        sum(cast(isnull(COINSRN_AMT ,'0.00')as money)) as [COINSRN_AMT],
        sum(cast(isnull(DDCTBL_AMT ,'0.00')as money)) as [DDCTBL_AMT],
        sum(cast(isnull(MBR_SNCTNS_PNLTYS_AMT ,'0.00')as money)) as [MBR_SNCTNS_PNLTYS_AMT],
        sum(cast(isnull(PAT_PAID_DFRNTL_AMT ,'0.00')as money)) as [PAT_PAID_DFRNTL_AMT],
        DISPENSE_AS_WRITTEN ,
        DISPENSE_DAYS_COUNT ,
        DRUG_STRENGTH ,
        MANUFACTURER_NAME ,
        INCURRED_YEAR_MONTH ,
        CLAIM_MAX_REVISION_DATE ,
        LAST_CLAIM_ADJUSTMENT ,
        PRIMARY_CARRIER_RESPONSIBLE_FOR_CLAIM ,
        ADJUDICATION_DATE ,
        CLAIM_PROCESSING_DATE
        CLAIM_ACTION_TYPE ,
        NPI ,
        PATIENT_ACCOUNT_NUMBER ,
        STATUS_CODE,
        @bcbs_load_date
FROM    tbl_claims_bcbs_pharmacy_stage_test AS a
WHERE   NOT EXISTS ( SELECT MASTER_CONSUMER_ID ,
                            CLAIM_NUMBER ,
                            CLAIM_ADJUSTMENT_NUMBER ,
                            CLAIM_DISPOSITION_CODE
                     FROM   tbl_claims_bcbs_pharmacy AS b
                     WHERE  a.MASTER_CONSUMER_ID = b.MASTER_CONSUMER_ID
                            AND a.CLAIM_NUMBER = b.CLAIM_NUMBER
                            AND a.CLAIM_ADJUSTMENT_NUMBER = b.CLAIM_ADJUSTMENT_NUMBER
                            AND a.CLAIM_DISPOSITION_CODE = b.CLAIM_DISPOSITION_CODE )

Group by  MASTER_CONSUMER_ID, claim_number, claim_adjustment_number, claim_disposition_code, filled_date, ndc_number, claim_processing_date, pharmacy_number , pharmacy_name ,therapeutic_class,
QUANTITY_DISPENSED,DAYS_SUPPLIED,dea_number, formulary_indicator ,retail_mail_indicator,[YEAR] ,quarter ,[MONTH], script_written_date , rx_number ,drug_name , dispensed_brand_generic_code ,generic_product_identifier,paid_date ,
claim_source_system, dispense_as_written , dispense_days_count , drug_strength , manufacturer_name , incurred_year_month, claim_max_revision_date , last_claim_adjustment ,
primary_carrier_responsible_for_claim , adjudication_date, claim_action_type , npi , patient_account_number , status_code  

 

 Error:

The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
0
Comment
Question by:Member_2_7969213
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points (awarded by participants)
ID: 41728138
the error is "clear".
your select has more or less fields

or it migth be in a trigger on the inserted table...

please just count ( i would really love the databases to provide the counts, but well)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41728144
and i counted 48 vs 50
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41728150
to troubleshoot, but both lists i t an excel file, side by side, to see which columns are mismatching
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:Member_2_7969213
ID: 41728225
Thank u Hengel

I counted and checked in excel too all there are 50 columns in both insert and select
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points (awarded by participants)
ID: 41728261
i think i found it
 CLAIM_PROCESSING_DATE
        CLAIM_ACTION_TYPE

a comma is missing there...
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 125 total points (awarded by participants)
ID: 41728266
You missed a comma after CLAIM_PROCESSING_DATE in the SELECT clause. Added that.
declare @bcbs_load_date char(8)
INSERT INTO tbl_claims_bcbs_pharmacy_test
(
MASTER_CONSUMER_ID,
CLAIM_NUMBER,
CLAIM_ADJUSTMENT_NUMBER, 
CLAIM_DISPOSITION_CODE, 
FILLED_DATE, 
NDC_NUMBER, 
DEA_NUMBER, 
PHARMACY_NUMBER,
PHARMACY_NAME, 
THERAPEUTIC_CLASS, 
QUANTITY_DISPENSED,
DAYS_SUPPLIED, 
FORMULARY_INDICATOR,
RETAIL_MAIL_INDICATOR,
BILLED_CHARGES, 
[YEAR],
QUARTER, 
[MONTH], 
PATIENT_RESPONSIBILITY_AMOUNT, 
PAID_AMOUNT, 
SCRIPT_WRITTEN_DATE, 
ALLOWED_AMT, 
RX_NUMBER, 
INGREDIENT_COST, 
COPAY, 
DRUG_NAME, 
DISPENSED_BRAND_GENERIC_CODE, 
GENERIC_PRODUCT_IDENTIFIER,
OTHER_PARTY_LIABILITY_AMOUNT,
PAID_DATE, 
CLAIM_SOURCE_SYSTEM, 
COINSRN_AMT, 
DDCTBL_AMT, 
MBR_SNCTNS_PNLTYS_AMT,
PAT_PAID_DFRNTL_AMT, 
DISPENSE_AS_WRITTEN, 
DISPENSE_DAYS_COUNT, 
DRUG_STRENGTH, 
MANUFACTURER_NAME,
INCURRED_YEAR_MONTH, 
CLAIM_MAX_REVISION_DATE,
LAST_CLAIM_ADJUSTMENT,
PRIMARY_CARRIER_RESPONSIBLE_FOR_CLAIM, 
ADJUDICATION_DATE, 
CLAIM_PROCESSING_DATE,
CLAIM_ACTION_TYPE, 
NPI, 
PATIENT_ACCOUNT_NUMBER, 
STATUS_CODE, 
month_ending
)
SELECT  MASTER_CONSUMER_ID ,
        CLAIM_NUMBER ,
        CLAIM_ADJUSTMENT_NUMBER ,
        CLAIM_DISPOSITION_CODE ,
        FILLED_DATE ,
        NDC_NUMBER ,
        DEA_NUMBER ,
        PHARMACY_NUMBER ,
        PHARMACY_NAME ,
        THERAPEUTIC_CLASS ,
        SUM(cast(isnull(QUANTITY_DISPENSED ,'0.00') as float)) as [QUANTITY_DISPENSED],
        SUM(cast(isnull(DAYS_SUPPLIED ,'0') as float)) as [DAYS_SUPPLIED],
        FORMULARY_INDICATOR ,
        RETAIL_MAIL_INDICATOR ,
        SUM(cast(isnull(BILLED_CHARGES, '0.00') as money)) as [BILLED_CHARGES] ,
        [YEAR] ,
        QUARTER ,
        [MONTH] ,
        SUM(cast(isnull(PATIENT_RESPONSIBILITY_AMOUNT, '0.00') as money)) as [PATIENT_RESPONSIBILITY_AMOUNT] ,
        SUM(cast(isnull(PAID_AMOUNT, '0.00') as money)) as [PAID_AMOUNT] ,
        SCRIPT_WRITTEN_DATE ,
        SUM(cast(isnull(ALLOWED_AMT, '0.00') as money)) as [ALLOWED_AMT] ,
        RX_NUMBER ,
        sum(cast(isnull(INGREDIENT_COST,'0.00') as money)) as [INGREDIENT_COST],
        sum(cast(isnull(COPAY,'0.00') as money)) as [COPAY],
        DRUG_NAME ,
        DISPENSED_BRAND_GENERIC_CODE ,
        GENERIC_PRODUCT_IDENTIFIER ,
        sum(cast(isnull(OTHER_PARTY_LIABILITY_AMOUNT ,'0.00')as money)) as [OTHER_PARTY_LIABILITY_AMOUNT],
        PAID_DATE ,
        CLAIM_SOURCE_SYSTEM ,
        sum(cast(isnull(COINSRN_AMT ,'0.00')as money)) as [COINSRN_AMT],
        sum(cast(isnull(DDCTBL_AMT ,'0.00')as money)) as [DDCTBL_AMT],
        sum(cast(isnull(MBR_SNCTNS_PNLTYS_AMT ,'0.00')as money)) as [MBR_SNCTNS_PNLTYS_AMT],
        sum(cast(isnull(PAT_PAID_DFRNTL_AMT ,'0.00')as money)) as [PAT_PAID_DFRNTL_AMT],
        DISPENSE_AS_WRITTEN ,
        DISPENSE_DAYS_COUNT ,
        DRUG_STRENGTH ,
        MANUFACTURER_NAME ,
        INCURRED_YEAR_MONTH ,
        CLAIM_MAX_REVISION_DATE ,
        LAST_CLAIM_ADJUSTMENT ,
        PRIMARY_CARRIER_RESPONSIBLE_FOR_CLAIM ,
        ADJUDICATION_DATE ,
        CLAIM_PROCESSING_DATE,
        CLAIM_ACTION_TYPE ,
        NPI ,
        PATIENT_ACCOUNT_NUMBER ,
        STATUS_CODE,
        @bcbs_load_date
FROM    tbl_claims_bcbs_pharmacy_stage_test AS a
WHERE   NOT EXISTS ( SELECT MASTER_CONSUMER_ID ,
                            CLAIM_NUMBER ,
                            CLAIM_ADJUSTMENT_NUMBER ,
                            CLAIM_DISPOSITION_CODE
                     FROM   tbl_claims_bcbs_pharmacy AS b
                     WHERE  a.MASTER_CONSUMER_ID = b.MASTER_CONSUMER_ID
                            AND a.CLAIM_NUMBER = b.CLAIM_NUMBER
                            AND a.CLAIM_ADJUSTMENT_NUMBER = b.CLAIM_ADJUSTMENT_NUMBER
                            AND a.CLAIM_DISPOSITION_CODE = b.CLAIM_DISPOSITION_CODE )

Group by  MASTER_CONSUMER_ID, claim_number, claim_adjustment_number, claim_disposition_code, filled_date, ndc_number, claim_processing_date, pharmacy_number , pharmacy_name ,therapeutic_class, 
QUANTITY_DISPENSED,DAYS_SUPPLIED,dea_number, formulary_indicator ,retail_mail_indicator,[YEAR] ,quarter ,[MONTH], script_written_date , rx_number ,drug_name , dispensed_brand_generic_code ,generic_product_identifier,paid_date ,
claim_source_system, dispense_as_written , dispense_days_count , drug_strength , manufacturer_name , incurred_year_month, claim_max_revision_date , last_claim_adjustment , 
primary_carrier_responsible_for_claim , adjudication_date, claim_action_type , npi , patient_account_number , status_code

Open in new window

0
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 125 total points (awarded by participants)
ID: 41729003
That's a consequence of optional keywords.

You can SELECT field1, field2 ..., you also SELECT field1 field2, but that is short for SELECT  field1 AS field2. If AS would not be optional, parsing that would point out an error of either a missing comma or missing AS.

So , if T-SQL would be more strict with such things, there'd be no chance for such an error, which only reveals if the number of fields is essential, manually inspecting just a SELECT having such an error you don't see a field missing in so many columns.

Since Guy talked of 48 vs 50, I'd expect another missing comma or any other missing field.

Bye, Olaf
0
 

Author Comment

by:Member_2_7969213
ID: 41731708
Friends thank You!! Yes Comma was missing
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSSQL Query for Selecting the SUM of a Specific Group 2 37
Format Output of Select Statement 2 38
What is needed to become a DBA? 7 56
Datatable / Dates ? 4 33
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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

752 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