Solved

SQL query

Posted on 2016-07-25
9
58 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
9 Comments
 
LVL 142

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 142

Expert Comment

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

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 142

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 40

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 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