Results 1 to 4 of 4
  1. #1
    Papa Echo is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2018
    Posts
    2

    Records are missing in a query and report

    Hello,
    I have two tables Tab1 and Tab2 which stand in a 1:n relationship connected over an ID. In Tab1 there is a field A. In Tab2 I have a field B. For a report I need the fields ID and A plus the sum of all B.
    My query in the report looks like this:
    SELECT Tab1.ID, Tab1.A, Sum(Tab2.B) AS C FROM Tab1 LEFT JOIN Tab2 ON Tab1.ID=Tab2.ID GROUP BY ID, A



    In the report I have a textfield: =[A] + Nz([C])

    My problem is that some records from Tab1 are missing if Tab2 doesn’t have data with the ID. But the LEFT JOIN means: Include ALL records from ‘Tab1’ and only those records from ‘Tab2’ where the joined fields are equal.

    What do I have to change to get all records from Tab1 even if there are no corresponding records in Tab2?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    you need to do them separately

    SELECT Tab1.ID, Tab1.A, C FROM Tab1 LEFT JOIN (SELECT ID, Sum(B) AS C FROM Tab2 GROUP BY ID) T3 ON Tab1.ID=T3.ID

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,972
    You haven't assigned an value in Nz if C is null
    May be worth doing the same for A as well though I assume that must always have a value

    Try
    Code:
    SELECT Tab1.ID, Tab1.A, Nz(Sum(Tab2.B),0) AS C FROM Tab1 LEFT JOIN Tab2 ON Tab1.ID=Tab2.ID GROUP BY ID, A
    then if it still fails
    Code:
    =[A] + Nz([C],0)
    Or
    Code:
    =Nz([A],0) + Nz([C],0)
    EDIT: Just read ajax's reply after I posted. I think his solution is the correct one
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    Papa Echo is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2018
    Posts
    2
    Thank you for the replies. Ajax is correct, I needed a subquery. It works now.
    And the default value of Nz([C]) is 0 if C is Null.

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

Similar Threads

  1. Replies: 12
    Last Post: 01-06-2017, 03:36 AM
  2. Replies: 2
    Last Post: 03-02-2015, 03:06 PM
  3. Missing records when query runs
    By SydB in forum Queries
    Replies: 6
    Last Post: 06-28-2014, 01:43 PM
  4. Data in query table missing from report
    By nick.muir2 in forum Reports
    Replies: 2
    Last Post: 07-10-2013, 05:18 PM
  5. Missing Records
    By colenzo in forum Access
    Replies: 3
    Last Post: 03-22-2012, 03:05 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