Results 1 to 9 of 9
  1. #1
    jpvonhemel is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    39

    Month Year Count Where No Records Exist for a Department

    I went to write a report that is grouped by MonthYear and shows the number of observations entered into a sharepoint survey. MonthYear is derived from the survey date so everything works when a record exists. My problem is trying to figure out how to group by Month/Year for those departments that have zero observations and thus zero records in my table. My desired output would be something like this.

    Department a

    Jan 2011 0
    Feb 2011 0


    Mar 2011 0

    and so on.

    Thank you,

    Jerold


    Code:
    SELECT qryHHSharepointSurveyLineListALL.MonthYear, tblSharepointHHDepartments.fldUnit, tblSharepointHHDepartments.fldDirector, Nz(Count([qryHHSharepointSurveyLineListALL].[ID]),0) AS Observations
    FROM tblSharepointHHDepartments LEFT JOIN qryHHSharepointSurveyLineListALL ON tblSharepointHHDepartments.fldUnit = qryHHSharepointSurveyLineListALL.fldDept
    GROUP BY qryHHSharepointSurveyLineListALL.MonthYear, tblSharepointHHDepartments.fldUnit, tblSharepointHHDepartments.fldDirector
    HAVING (((Nz(Count([qryHHSharepointSurveyLineListALL].[ID]),0))=0))
    ORDER BY Nz(Count([qryHHSharepointSurveyLineListALL].[ID]),0);

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    One method would require a table of departments and a dataset of all the possible month/year pairs. Join the two to have a dataset of all possible department/month/year combinations. Join that to the aggregate query with a join type of "Include all records from department/month/year ..."

    Another method uses DLookup in textboxes on report, one for each department and for each month/year.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    jpvonhemel is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    39
    Thank you June 7. I created a new query with depts/month/year (qryHHSharepointSurveyDeptMonthYearJoin) and then joined it to include all records from the new query. I am getting counts for those months that are > 0, but not the ones that equal 0.
    Code:
    SELECT qryHHSharepointSurveyDeptMonthYearJoin.MoYr, qryHHSharepointSurveyDeptMonthYearJoin.fldUnit, qryHHSharepointSurveyDeptMonthYearJoin.fldDirector, Nz(Count([qryHHSharepointSurveyLineListALL].[ID]),0) AS Observations, Month([MoYr]) AS MoNum, Year([MoYr]) AS YrNum
    FROM qryHHSharepointSurveyDeptMonthYearJoin LEFT JOIN qryHHSharepointSurveyLineListALL ON qryHHSharepointSurveyDeptMonthYearJoin.fldUnit = qryHHSharepointSurveyLineListALL.fldDept
    WHERE (((qryHHSharepointSurveyDeptMonthYearJoin.MoYr)=[MonthYear]))
    GROUP BY qryHHSharepointSurveyDeptMonthYearJoin.MoYr, qryHHSharepointSurveyDeptMonthYearJoin.fldUnit, qryHHSharepointSurveyDeptMonthYearJoin.fldDirector
    ORDER BY Nz(Count([qryHHSharepointSurveyLineListALL].[ID]),0);

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    How did you produce the dataset of all month/year pairs?

    Want to provide db for analysis? Follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    jpvonhemel is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    39
    I created a new table with a date type field. I added 1/1/13, 2/1/13, etc records. I then created a query with an expression to format 1/1/13 to Jan 2013. I created my dept/month/year dataset from both my original department table and the single field from month year query without using any relationships. That dataset had a unique month/year for each department. I will try to make a copy and anonymize data in the tables and provide a sample.

    Quote Originally Posted by June7 View Post
    How did you produce the dataset of all month/year pairs?

    Want to provide db for analysis? Follow instructions at bottom of my post.

  6. #6
    jpvonhemel is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    39
    Hi June7,

    Here is my db with a very modest record count. Chaplain has no observations, so I would like this listed as zero. My query is qryHHSharepointSurveyDeptNoResponseByMonth. I only see depts with records, as you should see.

    Thank you very much for helping me.

    Jerold

    Attachment 13715

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Your attachment link doesn't work, says 'Invalid Attachment'.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    jpvonhemel is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    39
    Quote Originally Posted by June7 View Post
    Your attachment link doesn't work, says 'Invalid Attachment'.
    That's odd. It worked last night. Let me try again. Hand Hygiene Anon.zip

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Building a table of all month/year pairs is one way to get that dataset. Also, if you are certain there will be at least one record for each month/year, that dataset can be generated by a query.

    For starters, remove all filter criteria from the queries.

    In qryHHSharepointSurveyDeptNoResponsesByMonth join MoYr to MonthYear as well as fldUnit to fldDept - this will be a compound join. The Observations count expression can be simpler, Nz not needed and just select Count on the Total Row: Count(qryHHSharepointSurveyLineListALL.ID) AS Observations

    Now view the results. This shows a record for each Unit for each month/year. Is the Observation data correct? For instance, Oct 2012 shows 2 South as the only Unit with an observation. The query is joining fldDept with fldUnit but the data is not consistent - fldDept has '1South' and fldUnit has '1 South'. How did this happen?

    You have ID field in tblSharepointHHDepartments defined as primary key but not using as foreign key. Make fldUnit the PK since this is the value saved in Surveys.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 05-26-2013, 03:28 PM
  2. Filter Form records By Month Year
    By maxmaggot in forum Forms
    Replies: 4
    Last Post: 04-05-2013, 06:34 PM
  3. search records by month and year
    By nurul in forum Forms
    Replies: 8
    Last Post: 12-09-2012, 09:34 PM
  4. show records in this month last year?
    By geoffcox in forum Queries
    Replies: 4
    Last Post: 06-11-2011, 07:12 AM
  5. Replies: 0
    Last Post: 03-25-2011, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums