Select All Dates in a Range in MS Access

99封情书 提交于 2019-12-12 05:33:59

问题


I have the following query in a MS Access 2013 DB that SELECTS a count of all records which have dates within the last 7 days:

SELECT DateValue([Proofpoint Attachment Defense.Received]) AS [Date],
       Count([Proofpoint Attachment Defense.Received]) AS [count]
FROM [Proofpoint Attachment Defense]
WHERE ((([Proofpoint Attachment Defense.Received])>=DateAdd("d",-7,Date())))
GROUP BY DateValue([Proofpoint Attachment Defense.Received])

This query produces the following result:

Date        count
10/19/2017    3
10/20/2017    2
10/25/2017    3

The result I need have to include all 7 dates even if there are no results:

Date        count
10/19/2017    3
10/20/2017    2
10/21/2017    0
10/22/2017    0
10/23/2017    0
10/24/2017    0
10/25/2017    3

回答1:


Create a query or table that produces dates covering your entire range and apply the same filter as now.

Then create a new query with table/query as source and with an left outer join to your query above on the field Date.

That will return your count as now and Null for the missing dates. Use Nz(CountField, 0) if you want zeroes for Null.

Addendum:

A query for generating a series of dates can be found here



来源:https://stackoverflow.com/questions/46954427/select-all-dates-in-a-range-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!