添加一个表列值和总和应保存在其他表的同一列中

时间:2015-07-16 19:07:53

标签: sql sql-server tsql

我使用的程序是

INSERT INTO Tbl_PPACA_Report(ssn,lastname,firstname,level1_code,level1,level2_Code,level2,level3_Code,level3,level4_Code,level4,level5_Code,level5,DOH,UNIONCode,PPE,Hours,Grosswages,EmployerId,PayrollActivityDate) 
            SELECT Pr.ssn,Pr.lastname,Pr.firstname,level1code,level1description,level2Code,level2description,level3Code,level3description,level4Code,level4description,level5Code,level5description,DateOfHire,ISNULL(Pr.UNIONCode,'') UNIONCode,Pr.PPE,Pr.Hours,Pr.Grosswages,Pr.EmployerId,Pr.PayrollActivityDate
            from Tbl_PPACA_Import PR 
            LEFT OUTER JOIN Tbl_PPACA_Report P ON P.SSN=PR.SSN and P.EmployerID=PR.EmployerId and 
            ISNULL(P.Level1_code,'')=ISNULL(PR.LEVEL1CODE,'') and ISNULL(P.Level2_code,'')=ISNULL(PR.LEVEL2CODE,'') 
            and ISNULL(p.Level3_code,'')=ISNULL(Pr.LEVEL3CODE,'') and ISNULL(p.Level4_code,'')=ISNULL(Pr.LEVEL4CODE,'') 
            and ISNULL(p.Level5_code,'')=ISNULL(Pr.LEVEL5CODE,'') and P.DOH=PR.DATEOFHIRE and P.PPE=PR.PPE
            and P.[HOURS]=PR.[HOURS] and P.[GrossWages]=PR.[GrossWages]

            where (p.DOH is null OR P.PPE is null  OR P.[HOURS] is null OR P.[GrossWages] is null ) and pr.EmployerId=@Employer_Id and PR.SSN is not null and PR.PPE is not null and PR.DATEOFHIRE is not null and ISNUMERIC(PR.SSN)=1  ;

所以我想要来自tbl_ppaca_import的hours列,并在Tbl_ppaca_report Hours列中保存此列的总和。 感谢

1 个答案:

答案 0 :(得分:0)

INSERT INTO Tbl_PPACA_Report(ssn,lastname,firstname,level1_code,level1,level2_Code,level2,level3_Code,level3,level4_Code,level4,level5_Code,level5,DOH,UNIONCode,PPE,Hours,Grosswages,EmployerId,PayrollActivityDate) 
        SELECT Pr.ssn,Pr.lastname,Pr.firstname,level1code,level1description,level2Code,level2description,level3Code,level3description,level4Code,
        level4description,level5Code,level5description,DateOfHire,ISNULL(Pr.UNIONCode,'') UNIONCode,Pr.PPE,
        sum(Pr.Hours),sum(Pr.Grosswages),Pr.EmployerId,Pr.PayrollActivityDate
        from Tbl_PPACA_Import PR 
        LEFT OUTER JOIN Tbl_PPACA_Report P ON P.SSN=PR.SSN and P.EmployerID=PR.EmployerId and 
        ISNULL(P.Level1_code,'')=ISNULL(PR.LEVEL1CODE,'') and ISNULL(P.Level2_code,'')=ISNULL(PR.LEVEL2CODE,'') 
        and ISNULL(p.Level3_code,'')=ISNULL(Pr.LEVEL3CODE,'') and ISNULL(p.Level4_code,'')=ISNULL(Pr.LEVEL4CODE,'') 
        and ISNULL(p.Level5_code,'')=ISNULL(Pr.LEVEL5CODE,'') and P.DOH=PR.DATEOFHIRE and P.PPE=PR.PPE
        and P.[HOURS]=PR.[HOURS] and P.[GrossWages]=PR.[GrossWages]
        where (p.DOH is null OR P.PPE is null  OR P.[HOURS] is null OR P.[GrossWages] is null or pr.PPE is not null ) and pr.EmployerId=@Employer_Id and PR.SSN is not null and PR.PPE is not null and PR.DATEOFHIRE is not null and ISNUMERIC(PR.SSN)=1 
        group by Pr.ssn,Pr.lastname,Pr.firstname,level1code,level1description,level2Code,level2description,level3Code,level3description,level4Code,level4description,level5Code,level5description,DateOfHire,Pr.UNIONCode,Pr.PPE,
        Pr.EmployerId,Pr.PayrollActivityDate  ;