Results 1 to 7 of 7
  1. #1
    clew3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    8

    Summing when no records meet criteria

    I have a query that sums up prices for items that meet certain criteria. It works fine when there are records that meet the criteria. However, when there are no records that meet the criteria I want it to return with a sum of zero. This zero gets used in other queries/reports. I've tried multiple ways of fixing this (Nz, IIf, etc.), yet nothing seems to work.

    Here is the SQL code from the query:
    SELECT [Account Numbers].[Account Number], Sum(Orders.[Total Item Price]) AS [SumOfTotal Item Price]
    FROM (Orders INNER JOIN [Collection Development Request] ON Orders.[Bib ID] = [Collection Development Request].ID) INNER JOIN [Account Numbers] ON [Collection Development Request].Type = [Account Numbers].[Item Type]
    GROUP BY [Account Numbers].[Account Number], Orders.Ordered, Orders.Received
    HAVING ((([Account Numbers].[Account Number])=61000) AND ((Orders.Ordered)=Yes) AND ((Orders.Received)=No));

    And here is a screenshot of the display view:
    Click image for larger version. 

Name:	Capture.JPG 
Views:	19 
Size:	55.7 KB 
ID:	9809

    Any help is appreciated. Please let me know if you need more info.

    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What items do you want to return a record for even if related data is not existing?

    Try changing one or both of the jointypes from INNER to LEFT or RIGHT.
    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
    clew3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    8
    I don't want to return any items. I just need to know price totals for items with matching criteria. If there are no items with matching criteria, I need it to sum a price total of $0.00.

    Changing the jointype doesn't help, unless that is the reason why one of the expressions I've tried (Nz, IIf) doesn't work.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    True, changing join type probably won't really help if the Yes and No criteria are used.

    Can't sum or count something that isn't there. So remove criteria then can use IIf or Nz expressions.

    Nz([Total Item Price],0)
    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
    clew3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    8
    But if I remove criteria how do I get it to sum only certain records when there are records that match the criteria?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Try creating a field with IIf expression then Sum that field.

    IIf(Ordered=Yes AND Received = No, [Total Item Price], 0)

    Instead of grouping in a query you might consider doing the grouping and sorting with aggregate calcs in a report. This will allow display of detail records and summary data.
    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
    clew3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    8
    That worked! I thought it was something along those lines, but I just wasn't sure how to build the IIf expression. Thanks!

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

Similar Threads

  1. Replies: 9
    Last Post: 09-22-2012, 02:04 PM
  2. Replies: 9
    Last Post: 06-28-2012, 10:10 PM
  3. Summing Sums in records...?
    By Hrtlover in forum Reports
    Replies: 33
    Last Post: 04-16-2011, 09:38 PM
  4. Need Help Summing UNIQUE Records
    By Millerguitarworks in forum Access
    Replies: 5
    Last Post: 05-27-2009, 04:37 PM
  5. Replies: 2
    Last Post: 03-31-2009, 11:15 AM

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