Results 1 to 10 of 10
  1. #1
    Abhi24 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2016
    Posts
    6

    URGENT: Handling Null in Sum of integer field

    Hi,

    I have a table in access 2007 which contains 3 columns - Model number, date, quantity(numeric field).
    There is another list of model numbers and the sum of quantity for each of these model numbers has to be calculated between 2 given dates.
    The problem is if the model number does not appear in the table, my query returns blank.



    I tried ISNULL(SUM(Quantity)) , SUM(Quantity) IS NULL and SUM(NZ(Quantity)) but nothing is working. I keep getting blank as answer.

    I need to complete this project on an urgent basis. Please help.

  2. #2
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    Are you using an Aggregate query?

  3. #3
    Abhi24 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2016
    Posts
    6
    Let me tell you exactly what I am doing here..
    There are 2 tables -
    1) Sales : having fields - Material, Cdate, Quantity
    2) Goods : having fields - Code, Stock
    [Code] is same as [Material]. But there can be more values of [Code] in FG compared to [Material] in Sales. So, all the [Material] values in Sales will map to [Code] in Goods table

    This is the original query:

    [CODE]SELECT (SUM(t1.[Quantity]) + (Select Sum([Stock]) From [Goods] where
    Code:
     = t1.Material)) AS Quantity FROM [Sales] AS t1 WHERE [Cdate] <=  #11/01/2016# AND [Cdate] >  #11/01/2015# GROUP BY Material;
    The problem is SUM(t1.[Quantity]) delivers NULL value in some cases which makes this whole query return 0 instead of (0+ Sum[Stock])

  4. #4
    Abhi24 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2016
    Posts
    6
    Sorry for the above error in posting..

    Let me tell you exactly what I am doing here..

    There are 2 tables -
    1) Sales : having fields - Material, Cdate, Quantity
    2) Goods : having fields - Code, Stock
    [Code] is same as [Material]. But there can be more values of [Code] in Goods compared to [Material] in Sales. So, all the [Material] values in Sales will map to [Code] in Goods table

    This is the original query:

    SELECT (SUM(t1.[Quantity]) + (Select Sum([Stock]) From [Goods] where [Code] = t1.Material)) AS Quantity FROM [Sales] AS t1 WHERE [Cdate] <= #11/01/2016# AND [Cdate] > #11/01/2015# GROUP BY Material;

    The problem is SUM(t1.[Quantity]) delivers NULL value in some cases which makes this whole query return 0 instead of (0+ Sum[Stock])

  5. #5
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    Try
    SELECT SUM(t1.[Quantity]) as exp1 (Select Sum([Stock]) From [Goods] where [Code] = t1.Material)) AS exp2, [exp1]+[exp2] as Exp3 FROM [Sales] AS t1 WHERE [Cdate] <= #11/01/2016# AND [Cdate] > #11/01/2015# GROUP BY Material;

    NOt tested. but in theory should add the values and provide them in Exp3.

  6. #6
    Abhi24 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2016
    Posts
    6
    I tried but it did not work. All 3 columns are blank. Any other ideas?

  7. #7
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    have you tried joining the two tables via Inner Join with Material = Code?

  8. #8
    Abhi24 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2016
    Posts
    6
    yes I did with no luck

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe use the NZ() function
    Code:
    SELECT (SUM(NZ(t1.[Quantity],0)) +  (Select Sum([Stock]) From [Goods] where  [C ode] = t1.Material)) AS Quantity FROM [Sales] AS t1 
    WHERE [Cdate]  <=  #11/01/2016# AND [Cdate] >  #11/01/2015# GROUP BY Material;

  10. #10
    Abhi24 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2016
    Posts
    6
    I had tried this earlier but it did not solve the issue. I guess one way out is to create another table Sales1 which has all the [Code] values in [Material] with 0 as quantity for the extra ones. Then I will rerun the query replacing Sales with Sales1.

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

Similar Threads

  1. Handling NULL with IIF statement
    By robbeh in forum Queries
    Replies: 7
    Last Post: 01-27-2016, 11:50 AM
  2. Handling Null In Concatenation
    By Juan4412 in forum Queries
    Replies: 5
    Last Post: 08-06-2015, 12:01 PM
  3. Null handling in access database
    By BatmanMR287 in forum Access
    Replies: 15
    Last Post: 01-13-2014, 03:20 PM
  4. Handling Null DLookup result
    By j6sander in forum Access
    Replies: 1
    Last Post: 07-19-2012, 11:07 AM
  5. Help with Null value handling
    By Thumbs in forum Programming
    Replies: 3
    Last Post: 03-05-2012, 10:55 AM

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