通过从多个表sql中选择值来更新表

时间:2013-10-07 12:48:23

标签: sql

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

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]

这里的问题是,有些表有很多行,有些表的行数较少。但是我在更新后为每个表获得的行数越少

我是否应该为每个选择表分别使用update。

3 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

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

USE 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]

答案 2 :(得分:0)

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