我有一个Access表,我可以在其中计算列的出现次数,但是除了计数之外,我还需要这些出现次数的百分比
我的表有两列。月,主题。我可以检索计数,例如表中的主题A出现了10次,主题B出现了15次,依此类推。
尝试了这两个代码,但给出了语法错误 代码1
SELECT Subjects,
COUNT(Subjects.Subject) AS CountOfSubject,
(SELECT COUNT(Subjects.Subject))/(SELECT SUM(Subjects.CountofSubject))) AS PercentofSubject
FROM Subjects
GROUP BY Subject;
代码2
SELECT Subjects,
COUNT(Subjects.Subject) AS CountOfSubject,
(SELECT AVG(Subjects.CountofSubject)) AS PercentofSubject
FROM Subjects
GROUP BY Subject;
但是,我需要附加一栏来显示这些主题发生的百分比值。非常感谢
答案 0 :(得分:0)
我在您的代码中看到的最大问题是,您没有通过使用完整的子查询来正确地规范计数。试试这个版本:
SELECT
Subject,
COUNT(*) AS CountOfSubject,
100.0 * COUNT(*) / (SELECT COUNT(*) FROM Subjects) AS PercentOfSubject
FROM Subjects
GROUP BY Subject;