How to find matching rows in two tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sadhana86
    New Member
    • Sep 2010
    • 22

    #1

    How to find matching rows in two tables

    I have two Tables say Table 1 and Table 2.
    Table 1 has following:

    ID Date Type
    1 20/09/2010 Invoice
    2 21/09/2010 Credit

    Table 2 has follwing:

    ID Date Type
    1 20/09/2010 Invoice
    2 21/09/2010 Credit

    So my Query has to give output if all the field that is ID,Date,Type are matching it has to throw a message box approved else it should say Not matching. Please help.
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    You haven't provided enough info to give a complete example

    will table2 be the table that has missing records that are in table 1

    or is it
    table1 that has the missing records

    or is it
    table1 might have records not in table2
    and table2 might also have records not in table1

    I will assume the easiest and you can adjust it to suit your situation

    Basically you join the 2 tables on each field and then test for null fields to determine if the record is missing or not

    Im going to assume that table1 always has every record and table2 might be missing some


    Code:
    SELECT a.ID,
           a.Date,
           a.Type,
           iif(b.ID is null,"Approved","Not Matching") as Status
    FROM Table1 a
    LEFT JOIN Table2 b
      ON a.ID=b.ID
     AND a.Date=b.Date
     AND a.Type=b.Type

    There are "find matching" and find unmatching query wizards that you can try.
    It will write a query similar to mine

    Comment

    • sadhana86
      New Member
      • Sep 2010
      • 22

      #3
      Hi Delerna,Many thanks for your reply. Unfortunately its the second situation you have mentioned.That is, table1 might have records not in table2
      and table2 might also have records not in table1.

      If there are no mismatch then it should throw a message "Approved" or it will show not matching and highlights the records that are not matching in both the tables.

      Please help.

      Comment

      • sadhana86
        New Member
        • Sep 2010
        • 22

        #4
        And If i give the above coding in the query design under expression it is not working. Kindly guide me. should I write a event on this.

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          then make a union query to get a single source of all records
          Code:
          SELECT *
          FROM Table1
          UNION select *
          from Table2
          Call this query qryCombined.



          now you want my first query but like this

          Code:
          SELECT a.ID, 
                 a.Date, 
                 a.Type, 
                 iif(b.ID is null or a.ID is null,"Not Matching","Approved") as Status 
          FROM qryCombined c
          LEFT JOIN Table1 a 
               ON c.ID=a.ID AND c.Date=a.Date AND c.Type=a.Type 
          LEFT JOIN Table2 b 
               ON c.ID=b.ID AND c.Date=b.Date AND c.Type=b.Type

          Comment

          • Delerna
            Recognized Expert Top Contributor
            • Jan 2008
            • 1134

            #6
            Or you can combine it all into one query

            Code:
            SELECT a.ID, 
                   a.Date, 
                   a.Type, 
                   iif(b.ID is null or a.ID is null,"Not Matching","Approved") as Status 
            FROM (SELECT * FROM Table1 UNION select * from Table2) c
            LEFT JOIN Table1 a 
                 ON c.ID=a.ID AND c.Date=a.Date AND c.Type=a.Type 
            LEFT JOIN Table2 b 
                 ON c.ID=b.ID AND c.Date=b.Date AND c.Type=b.Type


            Note, because I used UNION and not UNION ALL
            Each unique record will only appear once in the combined query

            Comment

            • Delerna
              Recognized Expert Top Contributor
              • Jan 2008
              • 1134

              #7
              This comment

              If there are no mismatch then it should throw a message "Approved"
              intrigues me.

              If you mean what I think you mean then you will probably need to resort to reading recordsets in VBA for that particular aspect

              Comment

              • sadhana86
                New Member
                • Sep 2010
                • 22

                #8
                Many thanks for your immediate response. Sorry but I am a beginner in access can u ellaborate by step by step like after i link columns in two table then step 2 like that.

                Comment

                • Delerna
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1134

                  #9
                  in access goto the query defs tab

                  create a new query

                  close the select tables window but keep the query editor window

                  In the toolbar at the top there is a dropdown with "Design View","SQL View" options

                  Select the SQL View option

                  Copy and paste my code into it
                  Code:
                  SELECT a.ID,  
                         a.Date,  
                         a.Type,  
                         iif(b.ID is null or a.ID is null,"Not Matching","Approved") as Status  
                  FROM (SELECT * FROM Table1 UNION select * from Table2) c 
                  LEFT JOIN Table1 a  
                       ON c.ID=a.ID AND c.Date=a.Date AND c.Type=a.Type  
                  LEFT JOIN Table2 b  
                       ON c.ID=b.ID AND c.Date=b.Date AND c.Type=b.Type

                  close and save the query




                  Providing the table and field names in your post are the actual names then hopefully the query will run

                  Comment

                  • sadhana86
                    New Member
                    • Sep 2010
                    • 22

                    #10
                    Hi There, I tries the above query in the SQl, However it is showing a error message showing "Syntax error (Missing Operator) in query expression".

                    Comment

                    • sadhana86
                      New Member
                      • Sep 2010
                      • 22

                      #11
                      Hi there, I tried the above query however it is showing a error message as "Syntax error (Missing Operator) in Query expression ".

                      Comment

                      • Delerna
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1134

                        #12
                        can you post your query

                        Comment

                        • sadhana86
                          New Member
                          • Sep 2010
                          • 22

                          #13
                          Hi There, I tried the below query and it is showing a error message as "Syntax error (Missing Operator) in Query expression ".

                          SELECT a.ID,
                          a.Date,
                          a.Type,
                          iif(b.ID is null or a.ID is null,"Not Matching","Appr oved") as Status
                          FROM (SELECT * FROM Table1 UNION select * from Table2) c
                          LEFT JOIN Table1 a
                          ON c.ID=a.ID AND c.Date=a.Date AND c.Type=a.Type
                          LEFT JOIN Table2 b
                          ON c.ID=b.ID AND c.Date=b.Date AND c.Type=b.Type

                          Comment

                          • sadhana86
                            New Member
                            • Sep 2010
                            • 22

                            #14
                            can some one help mw eith this. I want a Query, where it will compare two tables say Table1 and Table2. Both have same Fields - Date, Type and Name. if both the values in the tables are same then it should throw a message saying approved.table1 might have records not in table2
                            and table2 might also have records not in table1
                            If there are mismatch then mismatches should be come as a result of the query with the table name. Say record 1 is missing in Table1. can some help.
                            Note: As I am beginner request you to provide step by step reply. many thanks

                            Comment

                            • Delerna
                              Recognized Expert Top Contributor
                              • Jan 2008
                              • 1134

                              #15
                              Sorry, this thread got overlooked mixed in with all the others I am attempting to answer

                              try this
                              I rewrote it starting with the union query and then using the wizard to join the two tables

                              I don't know why Access objected to the original query???

                              Code:
                              SELECT c.ID, c.Date, c.Type, 
                                    IIf(Table2.ID Is Null Or Table1.ID Is Null,"Not Matching","Approved") AS Status
                              FROM (Table1  
                              RIGHT JOIN (SELECT * FROM Table1 UNION select * from Table2) AS c ON (Table1.Type = c.Type) AND (Table1.Date = c.Date) AND (Table1.ID = c.ID)) 
                              LEFT JOIN Table2  ON (c.Type = Table2.Type) AND (c.Date = Table2.Date) AND (c.ID = Table2.ID);

                              Comment

                              Working...