问题
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