合并一行中不同列中的计数值

时间:2017-05-05 00:45:52

标签: sql sql-server

这是代码和结果,我只需要显示一行... 任何建议谢谢。 enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

您的问题似乎有些错误,但我认为您要折叠数据,因此每个学院和部门只有一行。如果是这样,您可以使用max(),如下所示:

select
   College
  ,Department
  ,max(January) as January
  ,max(February) as February
  ,max(March) as March
from your_query_in_question     -- put your existing query here
group by
   College
  ,Department

答案 1 :(得分:0)

您可以尝试这样的SUM

SELECT  college,
        department,
        COUNT(department) as departmentCount,
        SUM(CASE when datepart(month,[date]) = 1 then 1 else 0 END) as [January],
        SUM(CASE when datepart(month,[date]) = 2 then 1 else 0 END) as [February]
From rpt_school
GROUP BY college,department