Please can someone help me with this Access query that I am trying to create?
I have a table with a list of projects and their start date and end date. What I need is a query that shows the breakdown of project days by month for each project, for example:
PROJECT Jan Feb Mar
AAAAA 7 28 2
...etc, where the numbers are the project days calculated based on the start date and end date. For example, in the above query, project AAAAA
would have a start date of 24/01/2012
and end date of 02/03/2012
Please could someone guide me on how to achieve this?
Many thanks!!
Suggest you look at crosstab queries: http://www.databasedev.co.uk/crosstab_queries.html
This shows exactly the kind of result you're looking for - counts per month. Note you may need to calculate days per month as an interim step eg by having a table per month to specify start and end dates you can then calculate how many days each project uses per month.
Your steps are something like:
1 table of date ranges per month
2 calculate start and end dates per project per month
In Access I think IIF is the equivalent to CASE?
You need to adjust the above depending on how you want to count the first /last days of a month (if a project finishes on the 31st does it count towards jan or does it need to continue to 1st feb etc) but its enough to get you started
Finally you use Access crosstab to transform this into the required format, doing date subtraction between the project start and end dates per month.
I created a calendar table to make this easier to cope with. I included the code for the two procedures I used (CreateTable_calendar and LoadCalendar) below. I added a "work_day" field to the calendar table in case you want to limit the count of days to only your organization's work days in each month. If so, you will need to adjust the query's WHERE clause accordingly. And also reset the work_day values for each calendar date if my choice doesn't match yours.
Anyway, I'll leave those details for you to sort out. Without making an adjustment for work vs. non-work days, this query returns the result set I think you want.
Notes:
Make the calendar table:
Load the calendar table. Without giving it an argument for year, it will load all dates for the current year. Otherwise it loads the dates for the year you supply as the argument.
Edit: Calendar is a reserved word. See Problem names and reserved words in Access. I didn't notice that until I examined my database with Mr. Browne's Database Issue Checker Utility. So I changed the name calendar to tblCalendar in this answer. And I strongly recommend that utility. In addition to identifying problems with reserved words, it can inform you about many other potential problem issues.