左外连接“相关名称'MMP_PPRVU'具有与表'MMP_PPRVU'相同的公开名称。”

时间:2014-11-04 14:49:36

标签: sql tsql

我一直在努力解决这个问题。这与我加入MMP_PPRVU的方式有关,我相信。基本上,我正在尝试加入CLARITY_TDL_TRAN - > CLARITY_EAP1 - > MMP_PPRVU。以及CLARITY_TDL_TRAN - > MMP_STAGING_UNITS - > MMP_PPRVU。

所有左外连接。我尝试了多种不同的方法,但无法提出有效的解决方案。非常感谢任何帮助。

SELECT........
FROM  

CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN  
left outer join 
dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
left outer join
CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
left outer join
CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
left outer join 
CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
left outer join
CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
left outer join
CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
left outer join
dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
left outer join
CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
left outer join
CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
left outer join
CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
left outer join 
dbo.MMP_PPRVU as MMP_PPRVU on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
left outer join 
dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
left outer join
MMP_PPRVU ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
  WHERE        (CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10)) AND       (mmp_PatientsTestaYes.PAT_ID IS NULL) AND (CLARITY_EAF_TIN.LINE = 1)

2 个答案:

答案 0 :(得分:2)

当您在FROM子句中为表提供别名时,将别名命名为与表名相同是非常容易的。

LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU
    ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD

此外,您在同一张桌子中加入了几个JOIN,它的名称是相同的。这两个表中的一个需要具有与表名不同的别名。也许:

LEFT OUTER JOIN dbo.MMP_PPRVU as MMP_PPRVU_1
    ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD

所以它不会与以下内容发生冲突:

LEFT OUTER JOIN MMP_PPRVU
     ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS

我发现在编写FROM子句时使用稍微不同的缩进很有帮助,这样这些东西会更突出,并且更容易解析。

FROM CLARITY.dbo.CLARITY_TDL_TRAN AS CLARITY_TDL_TRAN
    LEFT OUTER JOIN dbo.MMP_STAGING_UNITS AS MMP_STAGING_UNITS
        ON MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID
            AND CLARITY_TDL_TRAN.DETAIL_TYPE IN (
                1,
                10
                )
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP
        ON CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1
        ON CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_POS AS CLARITY_POS
        ON CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_DEP AS CLARITY_DEP
        ON CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_LOC AS CLARITY_LOC
        ON CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
    LEFT OUTER JOIN dbo.mmp_PatientsTestaYes AS mmp_PatientsTestaYes
        ON CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_SA AS CLARITY_SA
        ON CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_SER AS CLARITY_SER
        ON CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
    LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAF_TIN AS CLARITY_EAF_TIN
        ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
    LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU_1
        ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
    LEFT OUTER JOIN dbo.MMP_revctrs$ AS MMP_revctrs$
        ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
    LEFT OUTER JOIN MMP_PPRVU
        ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS

答案 1 :(得分:0)

我喜欢使用缩进来帮助保持联接可理解。

您可以在下面看到,您为CLARITY_EAP提供CLARITY_EAP1别名的第二次联接,但是当您尝试加入它时,您使用的是别名CLARITY_EAP_1,它没有被定义,可能会引发错误。

SELECT........
FROM CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN  
    left outer join dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
        left outer join dbo.MMP_PPRVU as MMP_PPRVU1 on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
    left outer join CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
        left outer join dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
    left outer join CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
        left outer join MMP_PPRVU as MMP_PPRVU2 ON CLARITY_EAP1.PROC_CODE = MMP_PPRVU2.HCPCS
    left outer join CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
    left outer join CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
    left outer join CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
        left outer join CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
    left outer join dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
    left outer join CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
    left outer join CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
WHERE
    CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10) AND
    mmp_PatientsTestaYes.PAT_ID IS NULL AND
    CLARITY_EAF_TIN.LINE = 1