Sql Server 2008 行列转换 不会用case when?

cooolchen cooolchen 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在AdventureWorks数据库中统计 Department 表中各个部门男女各有多少人?
涉及到的表有:
AdventureWorks.HumanResources.Employee
AdventureWorks.HumanResources.Department
AdventureWorks.HumanResources.EmployeeDepartmentHistory
我写的sql(有问题)

select b.Name, case c.Gender when 'M' then COUNT(c.EmployeeID) end as m,
case c.Gender when 'F' then COUNT(c.EmployeeID) end as f
from HumanResources.EmployeeDepartmentHistory a inner join HumanResources.Department b
on b.DepartmentID = a.DepartmentID
inner join HumanResources.Employee c
on a.EmployeeID = c.EmployeeID
group by b.DepartmentID,b.Name,c.Gender

得到的结果是


显然不对。
我想得到的结果如下:
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
cooolchen
疑难问题 22,298 成员 121,727 内容