【发布时间】:2020-06-27 23:48:41
【问题描述】:
我有以下疑问:
SELECT i.[Race / Ethnicity], NZ(Count(i.[Race / Ethnicity]),0) AS [Count Ethnicity]
FROM Information AS i
LEFT JOIN Visits AS v
ON i.[Customer ID] = v.[Customer ID]
WHERE (Month(v.[Visit Date]) = [Forms]![Report Generator]![month_box_rpt]
AND Year(v.[Visit Date]) = [Forms]![Report Generator]!
[year_box_rpt]) and (Month(i.[Signup Date]) = [Forms]![Report Generator]![month_box_rpt] AND Year(i.[Signup Date]) = [Forms]![Report Generator]![year_box_rpt])
GROUP BY (i.[Race / Ethnicity]);
它从表单中获取月份 ([month_box_rpt]) 和年份 ([year_box_rpt]) 值,并与其中一个表 ([Visit Date]) 中的值的月份和年份值进行比较,并返回不同人群的计数种族 (GROUP BY (i.[Race / Ethnicity]))。
查询有效。但是,如果有零个记录满足指定条件,则在运行查询时我会得到一个空白返回。
如果没有符合标准的种族的人,我如何获得包含我的每个种族和零值的结果?
例子:
Race / Ethnicity Count
Asian 0
Black 0
White 0
或(如果某些组有值):
Race / Ethnicity Count
Asian 0
Black 3
White 2
现在上面的查询返回:
Race / Ethnicity Count
或:
Race / Ethnicity Count
Black 3
White 2
我相信它涉及到一个子查询,并且已经尝试了几个,但都无法让它工作。
提前感谢您的指导。
【问题讨论】:
-
要么您需要每个种族/日期的 0 值虚拟记录,要么加入(LEFT 或 RIGHT,不是 INNER)GROUP BY 查询到种族表。
标签: ms-access group-by subquery