从Oracle查询中删除重复的结果

时间:2018-06-28 21:50:58

标签: oracle plsql duplicates union plsqldeveloper

预先感谢您的帮助。
这是提供额外结果的查询:

SELECT DISTINCT npi.id_provider AS npi_provider,
  mcnpi.num_contract_max                   AS num_contract_max,
  mcd.id_provider                          AS mcd_provider,
  provservloc.dte_effective                AS dte_effective,
  provservloc.dte_end                      AS dte_end,
--Case modified to pull condition from t_pmp_svc_loc for when dte_end > today
CASE
    WHEN provservloc.dte_end > 20180611 THEN 
        CASE WHEN mcfte.dte_end > 20180611 THEN mcfte.num_hrs_week ELSE 0 END
    ELSE 0 END                          AS num_hrs_week,
--Case added to handle Num_Current_Panel results
CASE
    WHEN provservloc.dte_end > 20180611 THEN
        CASE WHEN mcfte.dte_end > 20180611 THEN pmppanelrestrict.num_current_panel ELSE 0 END
    ELSE 0 END                       AS num_current_panel,
--Case added to handle Num_Future_Panel
CASE
    WHEN provservloc.dte_end > 20180611 THEN
        CASE WHEN mcfte.dte_end > 20180611 THEN pmppanelrestrict.num_future_panel ELSE 0 END
    ELSE 0 END                       AS num_future_panel,
--Case added to handle Num_Max_Panel numbers
CASE
    WHEN provservloc.dte_end > 20180611 THEN
        CASE WHEN mcfte.dte_end > 20180611 THEN pmppanelrestrict.num_max_panel ELSE 0 END
    ELSE 0 END                       AS num_max_panel,
  pmppanelrestrict.ind_panel_hold    AS ind_panel_hold,
  mcnpi.sak_npi              AS sak_npi ,
  provservloc.sak_prov,
  provservloc.sak_prov_loc
FROM t_pmp_svc_loc provservloc
  LEFT JOIN T_PR_IDENTIFIER MCD ON PROVSERVLOC.SAK_PROV_LOC = MCD.SAK_PROV_LOC AND PROVSERVLOC.SAK_PROV = MCD.SAK_PROV
  INNER JOIN t_pr_identifier npi on npi.sak_prov_loc = mcd.sak_prov_loc AND npi.sak_prov = mcd.sak_prov
  LEFT JOIN t_mc_fte mcfte ON provservloc.sak_prov = mcfte.sak_prov AND provservloc.sak_prov_loc = mcfte.sak_prov_loc
  LEFT JOIN t_mc_pmp_panel_restrict pmppanelrestrict ON provservloc.sak_pmp_ser_loc = pmppanelrestrict.sak_pmp_ser_loc
  LEFT JOIN t_mc_npi mcnpi ON mcnpi.sak_npi = mcd.sak_npi
  LEFT JOIN t_pr_php_elig elig ON provservloc.sak_prov_loc = elig.sak_prov_loc
WHERE 
PROVSERVLOC.SAK_PUB_HLTH       = 23
AND elig.sak_prov_pgm          = 57
AND elig.cde_enroll_status     IN ( 'A' ,'G', 'X' )
AND mcfte.cde_status1          != 'H'
AND mcd.cde_prov_id_type        = 'MCD'
AND NPI.CDE_PROV_ID_TYPE        = 'NPI'
AND npi.id_provider                = '123456789'

UNION

SELECT DISTINCT npi.id_provider AS npi_provider,
  mcnpi.num_contract_max                   AS num_contract_max,
  mcd.id_provider                          AS mcd_provider,
  provservloc.dte_effective                AS dte_effective,
  provservloc.dte_end                      AS dte_end,
NULL AS NUM_HRS_WEEK,
NULL AS num_current_panel,
NULL AS NUM_FUTURE_PANEL,
NULL AS num_max_panel,
NULL  AS ind_panel_hold,
  mcnpi.sak_npi              AS sak_npi ,
  provservloc.sak_prov,
  provservloc.sak_prov_loc
FROM t_pmp_svc_loc provservloc
  LEFT JOIN T_PR_IDENTIFIER MCD ON PROVSERVLOC.SAK_PROV_LOC = MCD.SAK_PROV_LOC AND PROVSERVLOC.SAK_PROV = MCD.SAK_PROV
  INNER JOIN T_PR_IDENTIFIER NPI ON NPI.SAK_PROV_LOC = MCD.SAK_PROV_LOC AND NPI.SAK_PROV = MCD.SAK_PROV
  LEFT JOIN t_mc_npi mcnpi ON mcnpi.sak_npi = mcd.sak_npi
  LEFT JOIN T_PR_PHP_ELIG ELIG ON PROVSERVLOC.SAK_PROV_LOC = ELIG.SAK_PROV_LOC
WHERE PROVSERVLOC.SAK_PROV  NOT IN (SELECT A.SAK_PROV FROM T_MC_FTE A INNER JOIN T_PMP_SVC_LOC B ON A.SAK_PROV = B.SAK_PROV)
AND PROVSERVLOC.SAK_PROV_LOC   not in (select a.sak_prov_loc from t_mc_fte a inner join t_pmp_svc_loc b on a.sak_prov_loc = b.sak_prov_loc)   
and provservloc.sak_pub_hlth       = 23
AND elig.sak_prov_pgm              = 57
AND ELIG.CDE_ENROLL_STATUS        IN ( 'A' ,'G', 'X' )
--AND mcfte.cde_status1          != 'H'
AND mcd.cde_prov_id_type        = 'MCD'
AND NPI.CDE_PROV_ID_TYPE        = 'NPI'
AND npi.id_provider                = '123456789';

结果列表我得到5行。看起来像这样:
Query_Result

我知道问题主要出在两个表上:
1. T_MC_FTE

SAK_PROV_LOC   SAK_PROV DTE_EFFECTIVE    DTE_END
------------ ---------- ------------- ----------
       32254      32254      20150401   20160331
       32254      32254      20171001   22991231
  1. T_PMP_SVC_LOC

    SAK_PROV_LOC   SAK_PROV DTE_EFFECTIVE    DTE_END
    ------------ ---------- ------------- ----------
           30859      30859      20050101   20090630
           32254      32254      20050401   20110630
           32254      32254      20150401   20160331
           32254      32254      20171001   22991231  
    

    我不需要的值是DTE_END = 20160331的T_MC_FTE表中的值。
    因此,基本上,我希望从T_PMP_SVC_LOC中获取所有结果,以及仅在今天T_MC_FTE.DTE_END>下与T_MC_FTE匹配的结果。 最后,结果应该是来自T_PMP_SVC_LOC的所有4行,并具有与T_MC_FTE中的DTE_END>今天匹配的一条记录相对应的适当列值;

关于如何改善UNION的任何建议?

1 个答案:

答案 0 :(得分:0)

最简单的解决方法是

select distinct npi_provider, 
                num_contract_max, 
                mcd_provider ...
from (
  your select statement 
)

但是,这可能会降低查询效率。尝试重新设计查询以完全不使用uniondistinct