Results 1 to 4 of 4
  1. #1
    mk01111000 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2020
    Posts
    5

    Comparing tables with Null values

    I am trying to compare two tables from different databases.
    I am aware that there are different ways of doing this and the one below may not be the most obvious but there are several things that need to be taken into consideration which would be to cumbersome to discuss here.


    The way I'd like to have it done is by concatenating fields and compare the results




    Code:
    SELECT x.* 
    FROM     ( SELECT * 
               FROM tblA 'C:\F1\db.mdb'
             ) x      
    LEFT JOIN 
            ( SELECT * 
              FROM tblA IN 'C:\F2\db.mdb'
             ) y 
    ON (x.fldId = y.fldId) 
    AND (x.fldInstrNr = y.fldInstrNr) 
    AND (x.fldCode = y.fldCode) 
    AND (x.fldQty = y.fldQty) 
    AND (x.fldUnit = y.fldUnit) 
    AND (x.fldType = y.fldType) 
    WHERE (
            ( x.ID & ';' & x.fldId & ';' & x.fldInstrNr & ';' & x.fldCode & ';' & x.fldQty  & ';' & x.fldUnit & ';' & x.fldType & ';' & x.zsfldDemo) <> 
            ( y.ID & ';' & y.fldId & ';' & y.fldInstrNr & ';' & y.fldCode & ';' & y.fldQty & ';' & y.fldUnit & ';' & y.fldType & ';' & y.zsfldDemo))

    This works, except with null-values in fields.


    Records with null-values will end up in the result although the record in both database are equal


    Is there a way to solve this, and preferably in a way more or less like the above?




    I tried the following but this takes very long to run (There are over 1000 records)
    Code:
    ON (Nz(x.fldId) = Nz(y.fldId)) AND (Nz(x.fldInstrNr) = Nz(y.fldInstrNr)) 
    AND (Nz(x.fldCode) = Nz(y.fldCode)) 
    AND (Nz(x.fldQty) = Nz(y.fldQty)) 
    AND (Nz(x.fldUnit) = Nz(y.fldUnit)) 
    AND (Nz(x.fldType) = Nz(y.fldType))

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    If the records are 'equal' then the WHERE clause should exclude them, even if they have null.

    Are you sure they are null in both? An empty string is not same as null.
    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
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    SELECT x.* 
    FROM     ( SELECT * 
               FROM tblA 'C:\F1\db.mdb'
             ) x      
    LEFT JOIN 
            ( SELECT * 
              FROM tblA IN 'C:\F2\db.mdb'
             ) y 
    ON (x.fldId = y.fldId) 
    AND (x.fldInstrNr = y.fldInstrNr) 
    AND (x.fldCode = y.fldCode) 
    AND (x.fldQty = y.fldQty) 
    AND (x.fldUnit = y.fldUnit) 
    AND (x.fldType = y.fldType) 
    WHERE ( x.ID & ';' & x.zsfldDemo) <> ( y.ID & ';' & y.zsfldDemo)
    Why wouldn't the WHERE clause be the bold line above?



    x.fldCode value
    y.fldCode value
    result
    record included in result set
    Null
    Null
    False No
    Null has value False No
    has value Null False No
    "Sue" "John" False No
    "Sue" "Sue" TRUE
    Yes
    has value zls False No


    So the other 5 fields in the join would be the same results.



    Due to the join clause, you know the 6 joining fields are equal for the record from "table x" to be included in the result set.
    So the only difference could be the the two fields that are not in the join clause - "ID" and "zsfldDemo"??


    Am I in bizzaro world?

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    I am trying to compare two tables from different databases.
    with what aim? what result do you want? return records in db x that don't match in db y (which is what you are trying to do)? the other way round? both? what are you going to do with the information once you have it?

    your code as presented will fail due to typo's. Which implies you have free typed it rather than use copy/paste. Aside from the fact that free typing takes longer, it gives me no confidence we are even looking at the real problem

    there are several things that need to be taken into consideration which would be to cumbersome to discuss here.
    if we knew those considerations, we might be able to suggest a better way.

    but that aside could perhaps make it simpler although it would still negate the use of indexes

    Code:
    SELECT x.* 
    FROM     ( SELECT *,  ID & ';' & fldId & ';' & fldInstrNr & ';' & fldCode & ';' & fldQty  & ';' & fldUnit & ';' & fldType & ';' & zsfldDemo as compare
    
               FROM tblA 'C:\F1\db.mdb'
             ) x      
    LEFT JOIN 
            ( SELECT * ,  ID & ';' & fldId & ';' & fldInstrNr & ';' & fldCode & ';' & fldQty  & ';' & fldUnit & ';' & fldType & ';' &  as compare
    
              FROM tblA IN 'C:\F2\db.mdb'
             ) y 
    ON (x.compare = y.compare)
    If this is a regular occurrence I would use a hash function on all the relevant fields and compare on that - probably only going to compare 8 or 10 chars rather the larger number you have at the moment. Perhaps even store the hash value in an additional indexed field.

    Another way to try using a non standard join (so can only be done in the sql window)

    Code:
    SELECT x.* 
    FROM     ( SELECT * 
               FROM tblA 'C:\F1\db.mdb'
             ) x      
    LEFT JOIN 
            ( SELECT * 
              FROM tblA IN 'C:\F2\db.mdb'
             ) y 
    ON (x.id<>y.id)
    AND (x.fldId = y.fldId) 
    AND (x.fldInstrNr = y.fldInstrNr) 
    AND (x.fldCode = y.fldCode) 
    AND (x.fldQty = y.fldQty) 
    AND (x.fldUnit = y.fldUnit) 
    AND (x.fldType = y.fldType) #
    AND (x.zsfldDemo<>y.zsfldDemo)
    other comment: your nz function should be a bit faster if you provide a default value should you choose to try that way again


    (Nz(x.fldId,0) = Nz(y.fldId,0))

    use 0 for numbers and '' for strings

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

Similar Threads

  1. Comparing values between fields
    By Spiken in forum Programming
    Replies: 14
    Last Post: 01-20-2022, 12:53 PM
  2. Comparing values in Access
    By mikesal57 in forum Access
    Replies: 18
    Last Post: 10-18-2021, 02:15 PM
  3. Replies: 2
    Last Post: 10-07-2019, 10:46 AM
  4. If statement comparing 2 values
    By Ekhart in forum Programming
    Replies: 3
    Last Post: 09-15-2016, 11:16 AM
  5. Replies: 1
    Last Post: 11-11-2015, 04:25 PM

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