Enumerate the Days (all the days) Between Two Dates (?)

C

croy

I have a table of surveys. The surveys are not done every
day. There is several years of data.

I need to show date for *every* day (whether a survey was
done or not), and show a total of survey details for each
day (even if it is zero).

Can I use a query to create a recordset that DateDiffs the
first & last survey dates, giving me a total of days between
the two dates, and then enumerate those days, as if it were
a table?
 
M

Marshall Barton

croy said:
I have a table of surveys. The surveys are not done every
day. There is several years of data.

I need to show date for *every* day (whether a survey was
done or not), and show a total of survey details for each
day (even if it is zero).

Can I use a query to create a recordset that DateDiffs the
first & last survey dates, giving me a total of days between
the two dates, and then enumerate those days, as if it were
a table?


You will need a table somewhere in the process. I always
include a utility table (named Numbers) with one (indexed)
long integer field (named Num) and populated with values
0,1,2,3, ... up to a few thousand. Use this table whenever
you need a list of made up values.

It's easy to get your min and max dates by using simple
subqueries:

SELECT DateAdd("d", Numbers.Num, (SELECT Min(X.surveydate)
FROM table As X)) As AllDates,
FROM Numbers
WHERE DateAdd("d", Numbers.Num, (SELECT Min(X.surveydate)
FROM table As X)) <= (SELECT Max(Y.surveydate)
FROM table As Y)

and then fit your survey details into that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top