我列出了他们的职业列表如下:
Doctor Male
Teacher Male
Principal Male
Nurse Female
Doctor Male
当我按职业分组时,我希望Access为任何不在我所拥有的子列表中的职业返回0。这是我正在使用的代码:
SELECT Profession.Profession, Count(Profession.Profession) AS CountOfProfession
FROM People INNER JOIN Profession ON People.PeopleID = Profession.PeopleID
WHERE (((People.Sex)="Male"))
GROUP BY Profession.Profession;
我希望它能为医生提供2,因为有2名男性,但由于没有男护士,因此护士也会收0。请让我知道如何做到这一点,谢谢!
答案 0 :(得分:0)
您需要外部联接。我改变了表的顺序,因此它是left join
:
SELECT Profession.Profession,
sum(iif(people.sex = "Male", 1, 0)) AS CountOfProfession
FROM Profession LEFT JOIN
ON People.PeopleID = Profession.PeopleID
GROUP BY Profession.Profession;
答案 1 :(得分:0)
这可以通过外连接并将条件从WHERE
移动到ON
子句来完成:
SELECT Profession.Profession,
COUNT(People.PeopleID) AS CountOfProfession
FROM Profession
LEFT JOIN People
ON ( People.PeopleID = Profession.PeopleID
AND People.Sex = 'Male' )
GROUP BY Profession.Profession ;