我有一个表格,其中有两列,一个是日期时间列(Test_Complete),另一个是字母数字记录ID列(RecordID)。
我需要准备一份按月处理的记录ID计数。我已经为此创建了一个查询。
SELECT (Format([Test_Complete],"mmm"" '""yy")) AS Evaluation_Month,
Count(tbl_TestStatus.Record_ID) AS CountOfRecord_ID
FROM tbl_TestStatus
WHERE (((tbl_TestStatus.[Test_Complete]) Is Not Null))
GROUP BY (Format([Test_Complete],"mmm"" '""yy")),
(Year([Test_Complete])*12+Month([Test_Complete])-1);
此查询效果很好,并为我提供如下输出:
Evaluation_Month CountOfRecord_ID
------------------ -----------------
Jan'12 20
Feb'12 90
Mar'12 40
Apr'12 50
现在我需要的是计算每个Evaluation_Month的CountOfRecord_ID值的百分比,并在Evaluation_Month数据中附加该值的百分比。
在上面的结果集中,所有CountOfRecord_ID的总和是200.因此需要计算百分比,将200计算为100%,这样我的结果如下所示:
Evaluation_Month CountOfRecord_ID
------------------ -----------------
Jan'12 (10%) 20
Feb'12 (45%) 90
Mar'12 (20%) 40
Apr'12 (25%) 50
如何修改我的SQL查询来实现这一目标?
答案 0 :(得分:3)
您只需在select
语句中添加一个“子查询字段”,其中包含记录总数。像这样:
SELECT
(Format([Test_Complete],"mmm"" '""yy")) AS Evaluation_Month,
Count(tbl_TestStatus.Record_ID) AS CountOfRecord_ID,
Count(tbl_TestStatus.Record_ID) / (select count(tbl_testStatus.recordId
from tbl_testStatus
where tbl_testStatus.test_complete is not null) as percent
FROM
tbl_TestStatus
WHERE
(((tbl_TestStatus.[Test_Complete]) Is Not Null))
GROUP BY
(Format([Test_Complete],"mmm"" '""yy")),
(Year([Test_Complete])*12+Month([Test_Complete])-1);