Help with query

  • I need to pull rows from view e if it has data if not pull data from view f.this is my query:

    SELECT

    c.copay_id,

    d.drug_label_assoc_detail_id,

    case

    when ((e.copay_flat_dollar_amount is NOT NULL) or (e.copay_percent is NOT NULL) or (e.dict_copay_display_id is NOT NULL)) then

    e.copay_pharmacy_type

    else

    f.copay_pharmacy_type

    end copay_pharmacy_type,

    case

    when ((e.copay_flat_dollar_amount is NOT NULL) or (e.copay_percent is NOT NULL) or (e.dict_copay_display_id is NOT NULL)) then

    e.copay_first_term

    else

    f.copay_first_term

    end copay_first_term

    FROM

    copay c with (nolock)

    cross join drug_label_assoc_detail d with (nolock)

    left outer join v_copay_drug_label_assoc_detail_2 e with (nolock)

    on c.copay_id =e.copay_id

    and e.drug_label_assoc_detail_id = d.drug_label_assoc_detail_id

    inner join dict_copay_display with (nolock)

    on e.dict_copay_display_id=dict_copay_display.dict_copay_display_id

    inner join health_care_plan with (nolock)

    on e.copay_id=health_care_plan.copay_id

    and e.dict_pbm_participant_id=health_care_plan.dict_pbm_participant_id

    and e.dict_formulary_dataload_source_code=health_care_plan.health_care_plan_dataload_source_code

    left outer join v_copay_summary_level f with (nolock)

    on c.copay_id=f.copay_id

    and f.drug_label_assoc_detail_id=d.drug_label_assoc_detail_id

    inner join dict_copay_display d1 with (nolock)

    on f.dict_copay_display_id=d1.dict_copay_display_id

    inner join health_care_plan h1 with (nolock)

    on f.copay_id=h1.copay_id

    and f.dict_pbm_participant_id=h1.dict_pbm_participant_id

    and f.dict_formulary_dataload_source_code=h1.health_care_plan_dataload_source_code

    this just returning rows common to e and f.What am I doing wrong?

    Thanks,

    SSM

  • Your outer joins are effectively being converted to inner joins by the subsequent inner joins.

    The joins should be nested so that the FROM clause looks something like:

    FROM

    &nbsp&nbsp&nbsp&nbspcopay C

    &nbsp&nbsp&nbsp&nbspCROSS JOIN drug_label_assoc_detail D

    &nbsp&nbsp&nbsp&nbspLEFT JOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspv_copay_drug_label_assoc_detail_2 E

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJOIN dict_copay_display DCD

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON DCD.dict_copay_display_id = E.dict_copay_display_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJOIN health_care_plan HCP

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON E.copay_id = HCP.copay_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND E.dict_pbm_participant_id = HCP.dict_pbm_participant_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND E.dict_formulary_dataload_source_code = HCP.health_care_plan_dataload_source_code

    &nbsp&nbsp&nbsp&nbsp)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON C.copay_id = E.copay_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND E.drug_label_assoc_detail_id = D.drug_label_assoc_detail_id

    &nbsp&nbsp&nbsp&nbspLEFT JOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspv_copay_summary_level F

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJOIN dict_copay_display D1

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON F.dict_copay_display_id = D1.dict_copay_display_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJOIN health_care_plan H1

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON F.copay_id = H1.copay_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND F.dict_pbm_participant_id = H1.dict_pbm_participant_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND F.dict_formulary_dataload_source_code = H1.health_care_plan_dataload_source_code

    &nbsp&nbsp&nbsp&nbsp)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON C.copay_id = F.copay_id

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND F.drug_label_assoc_detail_id = D.drug_label_assoc_detail_id

    ps The brackets are not required but are recommended for readability.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply