【问题标题】:How to add total row in SQL Server where the query contains case function如何在查询包含案例函数的 SQL Server 中添加总行
【发布时间】:2017-01-27 16:15:45
【问题描述】:

以下是我的查询和结果列,但不知道如何添加查询以获取总列。

Select  
    case PlacementVerificationmethod 
       when 107 then 'contact center'
       when 1 then 'work number/3rd party verification'
       when 101 then 'Placement call'
       when 102 then 'walk in/Self report'
       when 103 then 'Email'
       when 104 then 'Employer Report'
       when 105 then 'In person with participant'
       when 106 then 'In person with employer'
       else 'unknown'
    end  as 'Placement method', 
    count(*) as 'Total Placement' 
from
    AssessEmploymentPlacement
group by  
    PlacementVerificationMethod

结果:

In person with employer              145
Placement call                     13813
work number/3rd party verification  3492
Employer Report                     4463
In person with participant           168
unknown                            61387
walk in/Self report                 2227
Email                                115 

【问题讨论】:

    标签: sql sql-server count sum case


    【解决方案1】:

    如果你想要一个总,那么你可以这样做:

    Select (case PlacementVerificationmethod 
                when  107 then 'contact center'
                when 1 then 'work number/3rd party verification'
                when 101 then 'Placement call'
                when 102  then 'walk in/Self report'
                when 103 then 'Email'
                when 104 then 'Employer Report'
                when 105 then 'In person with participant'
                when 106 then 'In person with employer'
                else 'unknown'
            end) as [Placement method],
           count(*) as [Total Placement],
           sum(count(*)) over () as Total
    from AssessEmploymentPlacement
    group by PlacementVerificationMethod;
    

    如果您想要另一个 包含总数,则使用分组集或汇总:

    group by grouping sets ((PlacementVerificationMethod), ())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2013-09-23
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2019-10-31
      相关资源
      最近更新 更多