1

我正在使用来自多个表的值更新表。

UPDATE pv SET
            pv.[TotalDInTCG] = Dr.TCGDCnt
            ,pv.[AvgHbA1cImprovement] = hsd.AvgHbA1cCnt
            ,pv.[TotalCHDInTCG] = cpc.CHdCnt
            ,pv.[TotalCHDNTCG] = cpd1.CHdWithCholBPCnt
            ,pv.[PercentageOfCHDWithBPChol] = icp.CHFElligPopul
            ,pv.[TotalCOPDInTCG] = copd.TcgCOPDcount
            ,pv.[TotalCOPDMRCPOxySatuLevel] = copdmrc.TotalCOLevel
            ,pv.[PercentOferTCG] = copdOxSatu.oxySatligPopu
        FROM #tmpeTabel AS pv
            INNER JOIN #DRegistered AS Dr
            ON  Dr.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND Dr.Years = pv.[ReportYear]
                AND Dr.Months = pv.[ReportMonth]
            INNER JOIN #HbA1cScoreDetails AS hsd
            ON  hsd.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND hsd.Years = pv.[ReportYear]
                AND hsd.Months = pv.[ReportMonth]
            INNER JOIN #CHDCount as cpc
            ON  cpc.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND cpc.Years = pv.[ReportYear]
                AND cpc.Months = pv.[ReportMonth]
            INNER JOIN #CHDBPCholPatientsDetails as cpd1
            ON  cpd1.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND cpd1.Years = pv.[ReportYear]
                AND cpd1.Months = pv.[ReportMonth]
            INNER JOIN #ImprovementCHFPerPractice as icp
            ON  icp.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND icp.Years = pv.[ReportYear]
                AND icp.Months = pv.[ReportMonth]
            INNER JOIN #COPDCount as copd
            ON  copd.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND copd.Years = pv.[ReportYear]
                AND copd.Months = pv.[ReportMonth]
            INNER JOIN #COPDDetailsMRCOxygenSatuLevel as copdmrc
            ON  copdmrc.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND copdmrc.Years = pv.[ReportYear]
                AND copdmrc.Months = pv.[ReportMonth]
            INNER JOIN #ImprovPerElligPopula as copdOxSatu
            ON  copdOxSatu.SK_ServiceProviderID = pv.SK_ServiceProviderID
                AND copdOxSatu.Years = pv.[ReportYear]
                AND copdOxSatu.Months = pv.[ReportMonth]

这里的问题是,有些表有很多行,而有些表有更少的行。但是我在更新后得到的每张表的行数都变少了

我应该为每个选择表单独使用更新..

4

3 回答 3

0

假设您要更新的列都是可以pv为空的,这应该确保您捕获所有相关数据:

UPDATE pv SET
        pv.[TotalDInTCG] = Dr.TCGDCnt
        ,pv.[AvgHbA1cImprovement] = hsd.AvgHbA1cCnt
        ,pv.[TotalCHDInTCG] = cpc.CHdCnt
        ,pv.[TotalCHDNTCG] = cpd1.CHdWithCholBPCnt
        ,pv.[PercentageOfCHDWithBPChol] = icp.CHFElligPopul
        ,pv.[TotalCOPDInTCG] = copd.TcgCOPDcount
        ,pv.[TotalCOPDMRCPOxySatuLevel] = copdmrc.TotalCOLevel
        ,pv.[PercentOferTCG] = copdOxSatu.oxySatligPopu
    FROM #tmpeTabel AS pv
        LEFT JOIN #DRegistered AS Dr
        ON  Dr.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND Dr.Years = pv.[ReportYear]
            AND Dr.Months = pv.[ReportMonth]
        LEFT JOIN #HbA1cScoreDetails AS hsd
        ON  hsd.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND hsd.Years = pv.[ReportYear]
            AND hsd.Months = pv.[ReportMonth]
        LEFT JOIN #CHDCount as cpc
        ON  cpc.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND cpc.Years = pv.[ReportYear]
            AND cpc.Months = pv.[ReportMonth]
        LEFT JOIN #CHDBPCholPatientsDetails as cpd1
        ON  cpd1.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND cpd1.Years = pv.[ReportYear]
            AND cpd1.Months = pv.[ReportMonth]
        LEFT JOIN #ImprovementCHFPerPractice as icp
        ON  icp.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND icp.Years = pv.[ReportYear]
            AND icp.Months = pv.[ReportMonth]
        LEFT JOIN #COPDCount as copd
        ON  copd.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND copd.Years = pv.[ReportYear]
            AND copd.Months = pv.[ReportMonth]
        LEFT JOIN #COPDDetailsMRCOxygenSatuLevel as copdmrc
        ON  copdmrc.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND copdmrc.Years = pv.[ReportYear]
            AND copdmrc.Months = pv.[ReportMonth]
        LEFT JOIN #ImprovPerElligPopula as copdOxSatu
        ON  copdOxSatu.SK_ServiceProviderID = pv.SK_ServiceProviderID
            AND copdOxSatu.Years = pv.[ReportYear]
            AND copdOxSatu.Months = pv.[ReportMonth]
于 2013-10-07T12:55:17.260 回答
0

使用 LEFT JOIN 而不是 INNER JOIN 您可以更新该值,即使它与某些表不匹配。

使用 ISNULL 函数避免更新 PV 中的空值

UPDATE pv SET
                pv.[TotalDInTCG] = Dr.TCGDCnt
                ,pv.[AvgHbA1cImprovement] = hsd.AvgHbA1cCnt
                ,pv.[TotalCHDInTCG] = cpc.CHdCnt
                ,pv.[TotalCHDNTCG] = cpd1.CHdWithCholBPCnt
                ,pv.[PercentageOfCHDWithBPChol] = icp.CHFElligPopul
                ,pv.[TotalCOPDInTCG] = copd.TcgCOPDcount
                ,pv.[TotalCOPDMRCPOxySatuLevel] = copdmrc.TotalCOLevel
                ,pv.[PercentOferTCG] = copdOxSatu.oxySatligPopu
            FROM #tmpeTabel AS pv
                LEFT JOIN #DRegistered AS Dr
                ON  Dr.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND Dr.Years = pv.[ReportYear]
                    AND Dr.Months = pv.[ReportMonth]
                LEFT JOIN #HbA1cScoreDetails AS hsd
                ON  hsd.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND hsd.Years = pv.[ReportYear]
                    AND hsd.Months = pv.[ReportMonth]
                LEFT JOIN #CHDCount as cpc
                ON  cpc.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND cpc.Years = pv.[ReportYear]
                    AND cpc.Months = pv.[ReportMonth]
                LEFT JOIN #CHDBPCholPatientsDetails as cpd1
                ON  cpd1.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND cpd1.Years = pv.[ReportYear]
                    AND cpd1.Months = pv.[ReportMonth]
                LEFT JOIN #ImprovementCHFPerPractice as icp
                ON  icp.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND icp.Years = pv.[ReportYear]
                    AND icp.Months = pv.[ReportMonth]
                LEFT JOIN #COPDCount as copd
                ON  copd.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND copd.Years = pv.[ReportYear]
                    AND copd.Months = pv.[ReportMonth]
                LEFT JOIN #COPDDetailsMRCOxygenSatuLevel as copdmrc
                ON  copdmrc.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND copdmrc.Years = pv.[ReportYear]
                    AND copdmrc.Months = pv.[ReportMonth]
                LEFT JOIN #ImprovPerElligPopula as copdOxSatu
                ON  copdOxSatu.SK_ServiceProviderID = pv.SK_ServiceProviderID
                    AND copdOxSatu.Years = pv.[ReportYear]
                    AND copdOxSatu.Months = pv.[ReportMonth]
于 2013-10-07T12:58:56.427 回答
0

左外连接工作..主要问题是由于 INNER JOIN 的正常行为

于 2013-10-07T13:11:33.477 回答