Results 1 to 8 of 8
  1. #1
    Josha is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42

    Aggregate query - Nz function not working. I need to show a 0 for blank records

    Hi all,



    It's been a while since I have posted.

    I have an Access database that manages case notes for my student's examinations. They use it to create case notes, and then export them as 'reports' to PDF for me to review. However, the beauty of it, is that I have set up some queries so the students can run them to get the data they need from other student examinations and use that information for discussion points and conclusions.

    Recently, I created a query that would give the students a list of 'case numbers' and how many times a chemical batch was used - this information is stored in a separate linked table.

    I have noticed though that if a student doesn't use a chemical throughout their examination, the query doesn't even show case number at all. I want it to show the case number and a 0 for the chemical batch.

    So I thought I would design a simple aggregate query and use the Nz( ,0) function to do it. But when I run the query it still doesn't show me the case number and the 0 ! Its just ignored... Am I doing something wrong? The images below have some information blurred for privacy reasons...

    This is my query.....
    Click image for larger version. 

Name:	Screenshot 2023-05-11 084845.png 
Views:	23 
Size:	36.7 KB 
ID:	50223

    This is when I run it, its still not showing examinations with 0 they are just ignored!?
    Click image for larger version. 

Name:	Screenshot 2023-05-11 085306.png 
Views:	22 
Size:	21.4 KB 
ID:	50224

    I'm pretty sure Nz is the fuction /expression I need to use but its just not working

    Any help will be appreciated !

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Your query is showing a Count of Chemicals Used---the column colored blue in your graphic.

    What exactly do you want to display?

  3. #3
    Josha is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42
    Yes you are correct it is showing a count of chemicals used - which is what I want.... HOWEVER, if in the case, the student (caseworker) didn't use a chemical throughout the examination (ie they didn't record anything in the linked the table tbl_32_BatchNo), then in my query above I want it to show the case number and then a 0 in the Chemical Used column..... That's not what is happening right now, Access ignores that case number completely and doesn't show it.

    Does that make sense?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Case is ignored because there is no data for it. Can't aggregate data that doesn't exist. Build the aggregate query then join it to a dataset of all possible case numbers. And if you want the aggregation by case and worker, that will be a compound join to a dataset of all possible case and worker pairs.
    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
    Josha is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42
    Hey June7,

    I'm not sure what you mean build aggregate then join it to a dataset of all possible case numbers???

    Why can't access just see that there is no data for it and then tell me 0 instead of ignoring it? Is Nz ( ,) not the right function to use?

    THanks

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    No, Nz() cannot do what you want. As stated, there is no data to summarize so Nz() has no purpose in the aggregate query. No database can simply see what does not exist.

    Build the aggregate query like: SELECT CaseNum, WorkerID, Count(*) AS CntCaseByWorker FROM table GROUP BY CaseNum, WorkerID;

    Now you need a dataset of all possible case and worker pairs. How that dataset is generated is another topic, could be a Cartesian product: SELECT CaseNum, WorkerID, WorkerName FROM Cases, Workers;

    JOIN those two queries with compound link on CaseNum and WorkerID, possibly a LEFT JOIN on each link. Now Nz() can be used to show 0 for records where CntCaseByWorker field is null. Instead of using Nz() in query, could be expression in textbox on report.

    Be aware that queries using Cartesian product can perform slowly.

    This is a very common question in various forums.
    Last edited by June7; 05-11-2023 at 09:19 AM.
    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.

  7. #7
    Josha is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2019
    Location
    Victoria, Australia
    Posts
    42
    Hmmmm I don't know how to do that??

    Is there a step by step guide? ����

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I gave you the steps:

    1. build aggregate query

    2. build Cartesian query

    3. build query that joins first two

    Use query designer. View generated SQL in SQLView.
    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: 06-27-2021, 08:56 PM
  2. Query wont show records with a blank field
    By bignate in forum Queries
    Replies: 2
    Last Post: 09-03-2013, 04:45 AM
  3. Replies: 4
    Last Post: 05-11-2013, 07:51 AM
  4. Replies: 0
    Last Post: 04-25-2011, 07:58 PM
  5. Access SQL Query - Aggregate function
    By jack in forum Access
    Replies: 0
    Last Post: 11-10-2009, 08:06 PM

Tags for this Thread

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