我有一张叫Director Director的表
DirectorID FirstName FamilyName FullName DoB Gender
1 Steven Spielberg Steven Spielberg 1946-12-18 Male
2 Joel Coen Joel Coen 1954-11-29 Male
3 Ethan Coen Ethan Coen 1957-09-21 Male
4 George Lucas George Lucas 1944-05-14 Male
5 Ang Lee Ang Lee 1954-10-23 Male
6 Martin Scorsese Martin Scorsese 1942-11-17 Male
7 Mimi Leder Mimi Leder 1952-01-26 Female
我正在尝试确定女性导演与男性导演的比例
我可以使用以下方法计算出男导演和女导演的数量:
SELECT count(*) as myMale from Director
where Gender = 'Male'
SELECT count(*) as myFemale from Director
where Gender = 'Female')
但是我很难将它们结合起来以获得一定比例的女导演。
我在寻找结果:14.3% 使用以下方法计算的:
Total Female Directors / (Total Male Directors + Total Female Directors)
或
1/(6+1)
有人可以让我知道如何使用SQL吗?
谢谢
答案 0 :(得分:1)
一种简单的方法使用聚合。假设董事是男性还是女性(二元制),那么简单的条件聚集就足够了:
select avg(case when gender = 'Female' then 1.0 else 0 end) as ratio_female
from directors;
如果您只想将此限制为male
和female
(假设有其他选择),请添加where gender in ('female', 'male')
。