22,209
社区成员
发帖
与我相关
我的任务
分享
select b.Name,
sum(case when c.Gender ='M' and c.EmployeeID is not null then 1 else 0 end) as m,
sum(case when c.Gender ='F' and c.EmployeeID is not null then 1 else 0 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
/*
Name m f
-------------------------------------------------- ----------- -----------
Engineering 4 3
Tool Design 3 1
Sales 11 7
Marketing 5 5
Purchasing 9 4
Research and Development 2 2
Production 134 46
Production Control 6 0
Human Resources 4 2
Finance 5 6
Information Services 6 4
Document Control 4 1
Quality Assurance 6 1
Facilities and Maintenance 5 2
Shipping and Receiving 4 2
Executive 1 1
(16 行受影响)
*/
with t1 as(
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)
select name,max(m) 男,max(f) 女 from t1 group by name
/*
name 男 女
-------------------------------------------------- ----------- -----------
Document Control 4 1
Engineering 4 3
Executive 1 1
Facilities and Maintenance 5 2
Finance 5 6
Human Resources 4 2
Information Services 6 4
Marketing 5 5
Production 134 46
Production Control 6 NULL
Purchasing 9 4
Quality Assurance 6 1
Research and Development 2 2
Sales 11 7
Shipping and Receiving 4 2
Tool Design 3 1
警告: 聚合或其他 SET 操作消除了空值。
(16 行受影响)
*/
select b.Name, COUNT(case c.Gender when 'M' then c.EmployeeID ELSE NULL end) as m,
COUNT(case c.Gender when 'F' then c.EmployeeID ELSE NULL 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
with t1 as(
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)
select name,max(m) 男,max(f) 女 from t1
select b.Name,
max(case c.Gender when 'M' then COUNT(c.EmployeeID) end ) as m,
max( 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