SQL SUM查询

时间:2017-10-04 03:47:24

标签: sql

所以我是SQL /自学的新手,我已经构建了下面的代码,(我知道它的丑陋,但是它结合了google / stack over flow / me mucking through it,我需要SUM& #34; inforce,submit和deferred,在#step4中单独列中定义的评估中,我整天都在圈子里运行,所以如果有人能回答那就太酷了。我从SUM获得NULL行其他步骤工作正常。

SELECT DISTINCT #step1.staff_user_id,
                #step1.PBUID,
                #step1.staff_Branch,
                #step1.create_time,
                #step2.UserFullName,
                #step2.PositionName,
                Isnull(#step2.KBDepartmentName, 'Franchise') kb2,
                #step2.USERID                                AS userid2,
                #step2.EmployeeStatus,
                #step2.dss_current_flag,
                #step3.KBDivisionName,
                Sum(#step4.Inforce)                          AS Inforce,
                Sum(#step4.Submitted)                        AS Submitted,
                Sum(#step4.deferred)                         AS Deferred,
                Sum(#step4.[Under Assessment])               AS UnderAssessment
FROM   #step1
       LEFT JOIN #STEP2
              ON #step1.staff_user_id = #step2.UserId
       LEFT JOIN #STEP3
              ON #step1.staff_user_id = #step3.UserId
       LEFT JOIN #step4
              ON #step1.staff_user_id = #step4.Staff_user_Name
WHERE  #step2.dss_current_flag = 'Y'
       AND #step2.employeestatus = 'Active'
GROUP  BY #step1.staff_user_id,
          #step1.PBUID,
          #step1.staff_Branch,
          #step1.create_time,
          #step2.UserFullName,
          #step2.PositionName,
          Isnull(#step2.KBDepartmentName, 'Franchise'),
          #step2.USERID,
          #step2.EmployeeStatus,
          #step2.dss_current_flag,
          #step3.KBDivisionName 

0 个答案:

没有答案