计数员工性别的SQL查询

时间:2016-04-20 14:40:02

标签: sql

问题是:对于每个出版商列表,出版商名称,男性人数和出版商雇用的女性员工人数。 SQL语句应以以下格式显示结果:

Publishername    Male    Female
Academic            4   0
Ace Books           4   3
Oxford Press        0   4

从中获取信息的员工表是:

EMPNO LNAME                GENDER PUBLISHERNAME        SUPEREMPNO
----- -------------------- ------ -------------------- ----------
E1    Black                M      Pearson
E2    Clark                M      Pearson              E1
E3    Bean                 M      Pearson              E1
E4    Cassidy              M      Pearson              E1
E5    Smith                F      Elsevier
E6    Jones                F      Elsevier             E3
E7    Smith                F      Elsevier             E3
E8    West                 F      Elsevier             E3
E9    Smith                M      Planeta
E10   Love                 M      Planeta              E5
E11   Fish                 M      Planeta              E5
E12   Wood                 M      Planeta              E5
E13   Thorpe               F      Planeta              E5
E14   Gibson               M      Wiley
E15   Bergin               M      Wiley                E8
E16   Love                 F      Wiley                E8
E17   Owen                 F      Wiley                E8
E18   Haddock              F      Wiley                E8

18 rows selected.

由于

1 个答案:

答案 0 :(得分:2)

这是一个简单的条件聚合:

select publishername, 
       count(case when gender = 'M' then 1 end) as Male, 
       count(case when gender = 'F' then 1 end) as Female
from employee 
group by publishername
order by publishername;