SUM从CASE子句语句

时间:2016-05-20 17:34:20

标签: sql-server sum case distinct common-table-expression

我有以下查询。并且需要对查询“distinct case”值返回的所有内容进行求和,并在另一列“TotalDepartmentsCount”中填充一行

;WITH CTE
     AS 
     (
     Select t.ID As [CTE_ID]
           ,count(distinct case when e.Department='M' then t.ID else null end) as M_Marketing
           ,count(distinct case when e.Department='S' then t.ID else null end) as S_Sales   
           ,count(distinct case when e.Department='U' then t.ID else null end) as U_Utilization    
           ,count(distinct case when e.Department=' ' then t.ID else null end) as No_NoDepartment
        From dbo.Table t (nolock) 
            Left Join dbo.ClearedEmployee ce (nolock) ON t.ID = ce.building_fk
                 Join dbo.Employee e (nolock) ON ce.employee_fk = e.employee_pk
        Group By t.ID
    )

    Select *,  t.ID 
    From CTE c (nolock)
    FULL JOIN dbo.Table t (nolock) ON t.ID=c.[CTE_ID]
    Order By t.ID ASC;

为此,我在选择列表中添加了此代码:

    sum(cast(e.Department as int)) As TotalDepartmentsCount

但这并没有正确总结(见下面的截图)。请指教。 enter image description here

这就是我想要输出的方式(参见TotalDepartmentsCount表)

enter image description here

2 个答案:

答案 0 :(得分:1)

您的CTE目前通过基于部门的过滤来计算4个计数。你想要的是包括所有行的第五个计数(例如,没有过滤)。因此,您应该能够将该列添加到CTE中,如下所示:

;WITH CTE
 AS 
 (
 Select t.ID As [CTE_ID]
       ,count(distinct case when e.Department='M' then t.ID else null end) as M_Marketing
       ,count(distinct case when e.Department='S' then t.ID else null end) as S_Sales   
       ,count(distinct case when e.Department='U' then t.ID else null end) as U_Utilization    
       ,count(distinct case when e.Department=' ' then t.ID else null end) as No_NoDepartment
       ,count(distinct t.ID) as TotalDepartmentsCount
    From dbo.Table t (nolock) 
        Left Join dbo.ClearedEmployee ce (nolock) ON t.ID = ce.building_fk
             Join dbo.Employee e (nolock) ON ce.employee_fk = e.employee_pk
    Group By t.ID
)

只要Department列仅包含在不同情况下使用的四个特定值(' M',' S','你'和')。如果您有其他需要忽略的Department值,则可以更改CTE中的TotalDepartmentsCount列,如下所示:

,count(distinct case when e.Department in ('M', 'S', 'U', ' ') then t.ID else null end) as TotalDepartmentsCount

或者您可以简单地计算最终查询中四列的总和,如下所示:

Select c.*
    , (M_Marketing + S_Sales + U_Utilization + No_NoDepartment) as TotalDepartmentsCount
    , t.ID 
From CTE c (nolock)

答案 1 :(得分:1)

为什么不在计算完毕后将这些列添加到一起? e.g:

;WITH CTE
     AS 
     (
     Select t.ID As [CTE_ID]
           ,count(distinct case when e.Department='M' then t.ID else null end) as M_Marketing
           ,count(distinct case when e.Department='S' then t.ID else null end) as S_Sales   
           ,count(distinct case when e.Department='U' then t.ID else null end) as U_Utilization    
           ,count(distinct case when e.Department=' ' then t.ID else null end) as No_NoDepartment
        From dbo.Table t (nolock) 
            Left Join dbo.ClearedEmployee ce (nolock) ON t.ID = ce.building_fk
                 Join dbo.Employee e (nolock) ON ce.employee_fk = e.employee_pk
        Group By t.ID
    )

    Select *
        ,  t.ID
        , isnull(M_Marketing, 0) + isnull(S_Sales, 0) + isnull(U_Utilization, 0) + isnull(No_NoDepartment, 0) as TotalDepartments
    From CTE c (nolock)
    FULL JOIN dbo.Table t (nolock) ON t.ID=c.[CTE_ID]
    Order By t.ID ASC;