简体   繁体   English

在MS Access查询中返回没有结果的值

[英]Returning values in an MS access query that have no results

I have an MS Access 2013 query that runs automatically and shows the number of times certain incidents happen in each county. 我有一个自动运行的MS Access 2013查询,并显示每个县发生某些事件的次数。

The problem is that a few of the counties normally don't have any of these incidents so the query only shows counties with incidents. 问题在于,一些县通常没有任何这些事件,因此查询仅显示有事件的县。

I need to have the report list all counties even if the value is 0 . 即使值是0我也需要列出所有县的报告。 I know I'm missing something simple here, but I can't figure it out and all my research to find an answer has left me without an answer. 我知道我在这里遗漏了一些简单的东西,但是我无法弄清楚,我为寻找答案而进行的所有研究都使我无解。

I inherited this database, so it's not optimally designed. 我继承了此数据库,因此它的设计不是最佳的。 I'm also newer to access databases. 我也是访问数据库的新手。

I have checked all the options for the query in the design and SQL view (maybe I'm missing it.) 我已经在设计和SQL视图中检查了查询的所有选项(也许我错过了。)

Here is the entire code for the query: 这是查询的完整代码:

SELECT [FOCUS-CGDataComplete_By_Month/Year].COUNTY, Count([FOCUS- 
CGDataComplete_By_Month/Year].COUNTY) AS CT
FROM [FOCUS-CGDataComplete_By_Month/Year]
GROUP BY [FOCUS-CGDataComplete_By_Month/Year].COUNTY
ORDER BY Count([FOCUS-CGDataComplete_By_Month/Year].COUNTY) DESC;

The query runs correctly and returns all counties with incidents properly. 查询正确运行,并正确返回所有带事件的县。 So counties with incidents have the correct number, but counties with no incidents do not show (I know this is expected, but how do I 'override' that?) 因此发生事件的县的号码正确,但是没有发生事件的县却没有显示(我知道这是预期的,但是我如何“覆盖”那个?)

Firstly, you'll need a table containing all possible counties that you wish to see in the output - if there are counties which don't appear in any record in the table [FOCUS-CGDataComplete_By_Month/Year] , then this table obviously cannot report on counties of which it has no knowledge. 首先,您需要一个包含所有希望在输出中看到的县的表-如果表[FOCUS-CGDataComplete_By_Month/Year]任何记录中都没有出现县,则该表显然无法报告在它不知道的县。

Equipped with a table containing all possible counties, you can use a left join to output all counties in this table, alongside the count of records which appear in your [FOCUS-CGDataComplete_By_Month/Year] table associated with each county. 配备有包含所有可能县的表格,您可以使用left join输出该表中的所有县,以及与每个县相关的[FOCUS-CGDataComplete_By_Month/Year]表中显示的记录数。

Such a query could be something along the lines of the following: 这样的查询可能类似于以下内容:

select c.county, count(f.county) as ct
from countytable c left join [focus-cgdatacomplete_by_month/year] f on c.county = f.county
group by c.county
order by count(f.county) desc

Here, I assume that countytable is a table containing all possible values for the county field - you should rename this to suit the name of this table in your database. 在这里,我假设countytable是一个表,其中包含county域的所有可能值-您应该重命名它以适合数据库中该表的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM