473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Sep 22 '10 #1

✓ answered by Delerna

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???

Expand|Select|Wrap|Line Numbers
  1. SELECT c.ID, c.Date, c.Type, 
  2.       IIf(Table2.ID Is Null Or Table1.ID Is Null,"Not Matching","Approved") AS Status
  3. FROM (Table1  
  4. 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)) 
  5. LEFT JOIN Table2  ON (c.Type = Table2.Type) AND (c.Date = Table2.Date) AND (c.ID = Table2.ID);
  6.  

16 2332
Delerna
1,134 Expert 1GB
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


Expand|Select|Wrap|Line Numbers
  1. SELECT a.ID,
  2.        a.Date,
  3.        a.Type,
  4.        iif(b.ID is null,"Approved","Not Matching") as Status
  5. FROM Table1 a
  6. LEFT JOIN Table2 b
  7.   ON a.ID=b.ID
  8.  AND a.Date=b.Date
  9.  AND a.Type=b.Type
  10.  

There are "find matching" and find unmatching query wizards that you can try.
It will write a query similar to mine
Sep 23 '10 #2
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.
Sep 23 '10 #3
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.
Sep 23 '10 #4
Delerna
1,134 Expert 1GB
then make a union query to get a single source of all records
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM Table1
  3. UNION select *
  4. from Table2
  5.  
Call this query qryCombined.



now you want my first query but like this

Expand|Select|Wrap|Line Numbers
  1. SELECT a.ID, 
  2.        a.Date, 
  3.        a.Type, 
  4.        iif(b.ID is null or a.ID is null,"Not Matching","Approved") as Status 
  5. FROM qryCombined c
  6. LEFT JOIN Table1 a 
  7.      ON c.ID=a.ID AND c.Date=a.Date AND c.Type=a.Type 
  8. LEFT JOIN Table2 b 
  9.      ON c.ID=b.ID AND c.Date=b.Date AND c.Type=b.Type 
  10.  
Sep 23 '10 #5
Delerna
1,134 Expert 1GB
Or you can combine it all into one query

Expand|Select|Wrap|Line Numbers
  1. SELECT a.ID, 
  2.        a.Date, 
  3.        a.Type, 
  4.        iif(b.ID is null or a.ID is null,"Not Matching","Approved") as Status 
  5. FROM (SELECT * FROM Table1 UNION select * from Table2) c
  6. LEFT JOIN Table1 a 
  7.      ON c.ID=a.ID AND c.Date=a.Date AND c.Type=a.Type 
  8. LEFT JOIN Table2 b 
  9.      ON c.ID=b.ID AND c.Date=b.Date AND c.Type=b.Type 
  10.  
  11.  


Note, because I used UNION and not UNION ALL
Each unique record will only appear once in the combined query
Sep 23 '10 #6
Delerna
1,134 Expert 1GB
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
Sep 23 '10 #7
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.
Sep 23 '10 #8
Delerna
1,134 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. SELECT a.ID,  
  2.        a.Date,  
  3.        a.Type,  
  4.        iif(b.ID is null or a.ID is null,"Not Matching","Approved") as Status  
  5. FROM (SELECT * FROM Table1 UNION select * from Table2) c 
  6. LEFT JOIN Table1 a  
  7.      ON c.ID=a.ID AND c.Date=a.Date AND c.Type=a.Type  
  8. LEFT JOIN Table2 b  
  9.      ON c.ID=b.ID AND c.Date=b.Date AND c.Type=b.Type  
  10.  

close and save the query




Providing the table and field names in your post are the actual names then hopefully the query will run
Sep 23 '10 #9
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".
Sep 23 '10 #10
Hi there, I tried the above query however it is showing a error message as "Syntax error (Missing Operator) in Query expression ".
Sep 23 '10 #11
Delerna
1,134 Expert 1GB
can you post your query
Sep 23 '10 #12
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","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
Sep 24 '10 #13
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
Sep 27 '10 #14
Delerna
1,134 Expert 1GB
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???

Expand|Select|Wrap|Line Numbers
  1. SELECT c.ID, c.Date, c.Type, 
  2.       IIf(Table2.ID Is Null Or Table1.ID Is Null,"Not Matching","Approved") AS Status
  3. FROM (Table1  
  4. 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)) 
  5. LEFT JOIN Table2  ON (c.Type = Table2.Type) AND (c.Date = Table2.Date) AND (c.ID = Table2.ID);
  6.  
Sep 28 '10 #15
Wow excellent this is working now. Can we add the field table name in the query where it states from which table the mismatch is. Say Record 1, (date),(type) (Not matching), (Table Name)
Sep 28 '10 #16
Hi there, Now I am replacing the coding with my actual field and table names. My table1 name is Controloperator and Table 2 name is Teamleader. My fields in both the tables are: ID, Date completed, Type of Control.

SELECT c.ID, c.Date_completed, c.Type_of_Control, IIf(Teamleader.ID Is Null Or Controloperator.ID Is Null,"Not Matching","Approved") AS Status
FROM (Controloperator RIGHT JOIN (SELECT * FROM Controloperator UNION select * from Teamleader) AS c ON (Controloperator.Type_of_Control = c.Type_of_Control) AND (Controloperator.Date_completed = c.Date_completed) AND (Controloperator.ID = c.ID)) LEFT JOIN Teamleader ON (c.Type_of_Control = Teamleader.Type_of_Control) AND (c.Date_completed = Teamleader.Date_completed) AND (c.ID = Teamleader.ID);

But when I run this query it shows a error message showing "The Specified field 'Controloperator.Date_completed' could refer to more than one table listed in the FROM clause of your SQL statement. Please help
Sep 28 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Maciek Zywno | last post by:
Hello Experts ! I am to complete some tasks concerning nuumerical methods. I could use matlab to do it, but as I like java I wwould like to try to accomplish these tasks by means of java. I...
2
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
2
by: sumi | last post by:
I am very new to python , I have small query could some one help me. every time I get a new load i need to do few things like creating some dir, changing some file contents and moving some files ,...
3
by: Dave | last post by:
Hello. How can I set focus to some controls? For example I want to set focus to some textbox when the page is loaded or when I click on some button. And I have one more question: how can set...
3
by: Agnes | last post by:
In textbox, I know I can set e.handled = false in keypressevent , So I can restrict the user to input some invalid character or disallow the user to input any thing Now, in datagrid, there are 3...
2
by: Matthias Blohm | last post by:
Hello, a question about a tool or a possibility how could something work. following situation: we have a database which is full of very sensitive information and needed that db to use our online...
12
by: johannblake | last post by:
First off, I am NOT a beginner. I have lots of experience developing professional web sites and am a professional software developer. Unfortunately I've been out of web site development for the...
2
by: andre.lagerburg | last post by:
Does anyone know how to create a link (<a> . . . </a>) with some piece of javascript code that has the onClick event set for some function that I also wrote (not a build-in function). I can...
13
by: Praveen | last post by:
trying to learn plymorphism. My sample is public class Class1 { public static void Main(string args) { Cls1 x = new Cls1(); Cls2 y = new Cls2(); Cls3 y = new Cls3();
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.