如何有多个左外连接可能性

时间:2014-03-19 15:39:54

标签: sql

在这个查询中,我们将外部联接留给了处方表。

现在我了解到,有些记录不在此表中,基于NDC 但是基于SEQNO。

所以,如果你愿意的话,我们会有这种替代联盟。如何编码 如果找不到ndc上的连接,那么让我们加入SEQNO = SEQ_NO

"SELECT distinct f.FACILITY_NAME, ID_KEY, [BATCH] AS column1, [IMPORTDATE], [DATEBILLED], [RX], [DATEDISPENSED], [DAYSUPPLY], [PAYTYPE], [NPI], [PHYSICIAN], [COST], [QUANTITY], [MEDICATION], A.[NDC], " +
                    " case when COST > 0 then (COST / DAYSUPPLY) * 30 else 0 end [30DayCost] , [PATIENTNAME], [ROUTEOFADMIN], [INVOICECAT], [COPAY], [BRAND], [TIER], [SKILLLEVEL], [STAT] STATUS, [LASTTASKDATE],SEQNO,B.[SUBST_INSTRUCTIONS] , f.FACILITY_ID " +
                    " FROM [PBM].[T_CHARGES] A LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON A.[NDC] = B.[NDC]   Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI  Where [STAT] not in (3, 4, 5) AND SUBST_INSTRUCTIONS is not null AND [TIER] <> 'T1'  " +
                    sqlWhere + " AND f.FACILITY_ID IN (" + selected + ")";

2 个答案:

答案 0 :(得分:0)

使用union,您将获得相同的结果。

        "SELECT distinct f.FACILITY_NAME, ID_KEY, [BATCH] AS column1, 
    [IMPORTDATE], [DATEBILLED], [RX], [DATEDISPENSED], 
    [DAYSUPPLY], [PAYTYPE], [NPI], [PHYSICIAN], [COST], 
    [QUANTITY], [MEDICATION], A.[NDC], " 
    + " case when COST > 0 then (COST / DAYSUPPLY) * 30 else 0 end [30DayCost] ,
     [PATIENTNAME], [ROUTEOFADMIN], [INVOICECAT], [COPAY], [BRAND], [TIER], 
    [SKILLLEVEL], [STAT] STATUS, [LASTTASKDATE],SEQNO,B.[SUBST_INSTRUCTIONS] , f.FACILITY_ID "
    +" FROM [PBM].[T_CHARGES] A LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON A.[NDC] = B.[NDC]   Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI  Where [STAT] not in (3, 4, 5) AND SUBST_INSTRUCTIONS is not null AND [TIER] <> 'T1'  " 
    + sqlWhere + " AND f.FACILITY_ID IN (" + selected + ")" 

+
"UNION 
SELECT distinct f.FACILITY_NAME, ID_KEY, [BATCH] AS column1, 
    [IMPORTDATE], [DATEBILLED], [RX], [DATEDISPENSED], 
    [DAYSUPPLY], [PAYTYPE], [NPI], [PHYSICIAN], [COST], 
    [QUANTITY], [MEDICATION], A.[NDC], " 
    + " case when COST > 0 then (COST / DAYSUPPLY) * 30 else 0 end [30DayCost] ,
     [PATIENTNAME], [ROUTEOFADMIN], [INVOICECAT], [COPAY], [BRAND], [TIER], 
    [SKILLLEVEL], [STAT] STATUS, [LASTTASKDATE],SEQNO,B.[SUBST_INSTRUCTIONS] , f.FACILITY_ID "
    +" FROM [PBM].[T_CHARGES] A LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON SEQNO = SEQ_NO Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI  Where [STAT] not in (3, 4, 5) AND SUBST_INSTRUCTIONS is not null AND [TIER] <> 'T1'  " 
    + sqlWhere + " AND f.FACILITY_ID IN (" + selected + ")" 

答案 1 :(得分:0)

你应该可以使用另一个别名加入到处方表中。此外,因为你有&#34; AND B.SUBST_INSTRUCTIONS不为空&#34;在WHERE子句中,即将查询强制转换为INNER JOIN。我已将其移至左连接组件。紧接着,我添加了一个辅助左连接,但使用别名&#34; BySEQNO&#34; (类似的subst_inst不是null,而是通过别名)。

对于要检索的列,我应用COALESCE()基本上说明&#34; B中的值是否为空。&#34;别名,从&#34; BySEQNO&#34;中获取值。别名。

至于&#34; sqlwhere&#34;和#34;选择&#34;你在字符串中构建的值,我不知道这种影响。

最后,为了清楚地查询,你应该总是应用table.column(或者至少是alias.column)来提高可读性,好像一个列只对一个表是唯一的,引擎会知道并弄清楚它,但对于那些试图提供帮助,或者在您之后阅读,它不会成为该专栏的起源。

SELECT distinct 
      f.FACILITY_NAME, 
      ID_KEY, 
      BATCH AS column1, 
      IMPORTDATE, 
      DATEBILLED, 
      RX, 
      DATEDISPENSED, 
      DAYSUPPLY, 
      PAYTYPE, 
      NPI, 
      PHYSICIAN, 
      COST, 
      QUANTITY, 
      MEDICATION, 
      A.NDC,
      case when COST > 0 
         then (COST / DAYSUPPLY) * 30 
         else 0 end [30DayCost], 
      PATIENTNAME, 
      ROUTEOFADMIN, 
      INVOICECAT, 
      COPAY, 
      BRAND, 
      TIER, 
      SKILLLEVEL, 
      [STAT] STATUS, 
      LASTTASKDATE,
      SEQNO,
      COALESCE( B.SUBST_INSTRUCTIONS, BySEQNO.SUBST_INSTRUCTIONS ) SUBST_INSTRUCTIONS,
      f.FACILITY_ID 
   FROM 
      PBM.T_CHARGES A 
         LEFT OUTER JOIN OGEN.NDC_M_FORMULARY B 
            ON A.NDC = B.NDC
            AND B.SUBST_INSTRUCTIONS is not null 
         LEFT OUTER JOIN OGEN.NDC_M_FORMULARY BySEQNO
            ON A.SEQNO = BySEQNO.SEQ_No
            AND BySEQNO.SUBST_INSTRUCTIONS is not null 
         Left Outer Join PBM.FACILITY f 
            on A.FACILITYNPI = f.FACILITY_NPI  
   Where 
          [STAT] not in (3, 4, 5) 
      AND TIER <> 'T1'  
      sqlWhere 
      AND f.FACILITY_ID IN (" + selected + ")";