Results 1 to 4 of 4
  1. #1
    Kundan is offline Competent Performer
    Windows XP Access 2013 32bit
    Join Date
    Mar 2018
    Posts
    155

    Difference query


    In the attached DB I have 2 queries Qry_NIssuedSum & Qry_NRecvdSum. I want the difference of these two which I tried in Qry_NDiff. But only 2 records become available. The difference of record 7 doesn't come. Please guide me.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Change the JOIN types.

    SELECT Qry_NIssuedSum.SumOfAmount AS I, Qry_NRecvdSum.SumOfAmount AS R, Tbl_NTransac.ID, [R]-[I] AS D
    FROM (Qry_NIssuedSum RIGHT JOIN Tbl_NTransac ON Qry_NIssuedSum.ID = Tbl_NTransac.ID) LEFT JOIN Qry_NRecvdSum ON Tbl_NTransac.ID = Qry_NRecvdSum.ID;
    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
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    You are using INNER joins so only records common to both queries are shown.
    Also, I suggest you use the Nz function to convert null records to zero for clarity

    Doing these gives all 5 records
    Code:
    SELECT Nz([Qry_NIssuedSum].[SumOfAmount],0) AS I, Nz([Qry_NRecvdSum].[SumOfAmount],0) AS R, Tbl_NTransac.ID, [R]-[I] AS D
    FROM (Qry_NIssuedSum RIGHT JOIN Tbl_NTransac ON Qry_NIssuedSum.ID = Tbl_NTransac.ID) LEFT JOIN Qry_NRecvdSum ON Tbl_NTransac.ID = Qry_NRecvdSum.ID;
    However ID 5 & 6 have both items as null so you may wish to exclude those leaving ID 3,4,7
    Code:
    SELECT Nz([Qry_NIssuedSum].[SumOfAmount],0) AS I, Nz([Qry_NRecvdSum].[SumOfAmount],0) AS R, Tbl_NTransac.ID, [R]-[I] AS D 
    FROM (Qry_NIssuedSum RIGHT JOIN Tbl_NTransac ON Qry_NIssuedSum.ID = Tbl_NTransac.ID) LEFT JOIN Qry_NRecvdSum ON Tbl_NTransac.ID = Qry_NRecvdSum.ID
    WHERE (((Nz([Qry_NIssuedSum].[SumOfAmount],0))<>0)) OR (((Nz([Qry_NRecvdSum].[SumOfAmount],0))<>0));
    If you're not bothered about having null values shown as blank then use
    Code:
    SELECT Qry_NIssuedSum.SumOfAmount AS I, Qry_NRecvdSum.SumOfAmount AS R, Tbl_NTransac.ID, [R]-[I] AS D
    FROM (Qry_NIssuedSum RIGHT JOIN Tbl_NTransac ON Qry_NIssuedSum.ID = Tbl_NTransac.ID) LEFT JOIN Qry_NRecvdSum ON Tbl_NTransac.ID = Qry_NRecvdSum.ID
    WHERE (((Qry_NIssuedSum.SumOfAmount) Is Not Null)) OR (((Qry_NRecvdSum.SumOfAmount) Is Not Null));
    However fixing this doesn't affect the fact that your table structure is wrong
    Both tables tbl_NIssued & tbl_NReceived should be merged with + or - values for Amount
    Similarly you should not be saving the calculated differences in tbl_NTransac.
    These should just be calculated using a query as & when needed
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  4. #4
    Kundan is offline Competent Performer
    Windows XP Access 2013 32bit
    Join Date
    Mar 2018
    Posts
    155
    Quote Originally Posted by ridders52 View Post
    You are using INNER joins so only records common to both queries are shown.
    Also, I suggest you use the Nz function to convert null records to zero for clarity

    Doing these gives all 5 records
    Code:
    SELECT Nz([Qry_NIssuedSum].[SumOfAmount],0) AS I, Nz([Qry_NRecvdSum].[SumOfAmount],0) AS R, Tbl_NTransac.ID, [R]-[I] AS D
    FROM (Qry_NIssuedSum RIGHT JOIN Tbl_NTransac ON Qry_NIssuedSum.ID = Tbl_NTransac.ID) LEFT JOIN Qry_NRecvdSum ON Tbl_NTransac.ID = Qry_NRecvdSum.ID;
    However ID 5 & 6 have both items as null so you may wish to exclude those leaving ID 3,4,7
    Code:
    SELECT Nz([Qry_NIssuedSum].[SumOfAmount],0) AS I, Nz([Qry_NRecvdSum].[SumOfAmount],0) AS R, Tbl_NTransac.ID, [R]-[I] AS D 
    FROM (Qry_NIssuedSum RIGHT JOIN Tbl_NTransac ON Qry_NIssuedSum.ID = Tbl_NTransac.ID) LEFT JOIN Qry_NRecvdSum ON Tbl_NTransac.ID = Qry_NRecvdSum.ID
    WHERE (((Nz([Qry_NIssuedSum].[SumOfAmount],0))<>0)) OR (((Nz([Qry_NRecvdSum].[SumOfAmount],0))<>0));
    If you're not bothered about having null values shown as blank then use
    Code:
    SELECT Qry_NIssuedSum.SumOfAmount AS I, Qry_NRecvdSum.SumOfAmount AS R, Tbl_NTransac.ID, [R]-[I] AS D
    FROM (Qry_NIssuedSum RIGHT JOIN Tbl_NTransac ON Qry_NIssuedSum.ID = Tbl_NTransac.ID) LEFT JOIN Qry_NRecvdSum ON Tbl_NTransac.ID = Qry_NRecvdSum.ID
    WHERE (((Qry_NIssuedSum.SumOfAmount) Is Not Null)) OR (((Qry_NRecvdSum.SumOfAmount) Is Not Null));
    However fixing this doesn't affect the fact that your table structure is wrong
    Both tables tbl_NIssued & tbl_NReceived should be merged with + or - values for Amount
    Similarly you should not be saving the calculated differences in tbl_NTransac.
    These should just be calculated using a query as & when needed

    Thanks for your advice on table structure. I am working on it.

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

Similar Threads

  1. Need help with time difference query
    By christel in forum Queries
    Replies: 1
    Last Post: 05-20-2015, 05:16 AM
  2. Difference Query
    By kaushalneo in forum Queries
    Replies: 1
    Last Post: 05-20-2013, 01:49 PM
  3. SQL Query, find difference in two times.
    By Creaturemagic in forum Queries
    Replies: 7
    Last Post: 05-13-2013, 12:08 AM
  4. DateTime Difference Query help?
    By Astron2012 in forum Queries
    Replies: 6
    Last Post: 04-27-2012, 10:24 PM
  5. Replies: 1
    Last Post: 04-14-2011, 07:19 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