【发布时间】:2014-10-13 06:02:07
【问题描述】:
我目前在MS Access:
SELECT ClassID, Count(Component) AS ActiveDuty
FROM tblStudents
WHERE Component = "Active_Duty"
GROUP BY classID;
它给了我正确的答案,如下所示:
ClassID ActiveDuty
006-14 14
007-14 12
008-14 8
但如果我想让它看起来像这样,我需要做什么?
ClassID ActiveDuty Reserve National Guard
006-14 14 5 6
007-14 12 9 8
008-14 8 7 18
我尝试使用这样的子查询:
SELECT ClassID, (SELECT COUNT(Component) FROM tblStudents WHERE Component = "ActiveDuty") AS Active_Duty,(SELECT COUNT(Component) FROM tblStudents WHERE Component = "Reserve") AS ArmyReserve,
(SELECT COUNT(Component) FROM tblStudents WHERE Component = "National_Guard") AS NationalGuard
FROM tblStudents
WHERE Component = "Active_Duty"
GROUP BY ClassID;
但这是我得到的结果:
ClassID ActiveDuty Reserve National Guard
006-14 34 37 29
007-14 34 37 29
008-14 34 37 29
【问题讨论】:
-
也许您应该编辑您的问题并显示您尝试使用的子查询。