473,396 Members | 2,113 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 mismatches in two tables

can some one help me with this. I want a Query, where it will compare two tables say Table 1 as Controloperator and Table 2 as Teamleader. Both have same Fields - Date completed, Type of control and Name. if both the values in the tables are same then it should throw a message saying approved. 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.

Below is the Query :
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);

It is showing error message as "The Specified field 'Controloperator.Date_completed' could refer to more than one table listed in the FROM clause of your SQL statement. Kindly help. And I also need the table name from where the mismatch happend should also reflect in my query.
Sep 28 '10 #1

✓ answered by MMcCarthy

This should do it ...

Expand|Select|Wrap|Line Numbers
  1. SELECT c.ID, c.[Date completed], c.[Type of Control], "Approved" AS Status, "Both" AS TableName
  2. FROM Controloperator c INNER JOIN TeamLeader t
  3. ON c.[Type of Control] = t.[Type of Control]
  4. AND c.[Date completed] = t.[Date completed]
  5. AND c.ID = t.ID
  6. UNION
  7. SELECT c.ID, c.[Date completed], c.[Type of Control], "Not Matched" AS Status, "Controloperator" AS TableName
  8. FROM Controloperator c LEFT JOIN TeamLeader t
  9. ON c.[Type of Control] = t.[Type of Control]
  10. AND c.[Date completed] = t.[Date completed]
  11. AND c.ID = t.ID
  12. WHERE t.ID Is Null
  13. UNION
  14. SELECT t.ID, t.[Date completed], t.[Type of Control], "Not Matched" AS Status, "TeamLeader" AS TableName
  15. FROM TeamLeader t LEFT JOIN Controloperator c
  16. ON t.[Type of Control] = c.[Type of Control]
  17. AND t.[Date completed] = c.[Date completed]
  18. AND t.ID = c.ID
  19. WHERE c.ID Is Null

6 4173
MMcCarthy
14,534 Expert Mod 8TB
OK I'd take a completely different approach, your query is too complicated. I think the following will give you what you want.

Expand|Select|Wrap|Line Numbers
  1. SELECT c.ID, c.Date_completed, c.Type_of_Control, "Approved" AS Status
  2. FROM Controloperator c INNER JOIN TeamLeader t
  3. ON c.Type_of_Control = t.Type_of_Control
  4. AND c.Date_completed = t.Date_completed
  5. AND c.ID = t.ID
  6. UNION
  7. SELECT c.ID, c.Date_completed, c.Type_of_Control, "Not Matched" AS Status
  8. FROM Controloperator c LEFT JOIN TeamLeader t
  9. ON c.Type_of_Control = t.Type_of_Control
  10. AND c.Date_completed = t.Date_completed
  11. AND c.ID = t.ID
  12. WHERE t.ID Is Null
  13. UNION
  14. SELECT t.ID, t.Date_completed, t.Type_of_Control, "Not Matched" AS Status
  15. FROM TeamLeader t LEFT JOIN Controloperator c
  16. ON t.Type_of_Control = c.Type_of_Control
  17. AND t.Date_completed = c.Date_completed
  18. AND t.ID = c.ID
  19. WHERE c.ID Is Null
The first query will give all records that are matching in both tables. The second will give all records in Controloperator without match in TeamLeader and the third will give all records in TeamLeader without a match in Controloperator.
Sep 29 '10 #2
Many thanks for your response, However it is asking for the parameter value for date completed and Type of control. But both the tables can have different dates and different types of control. For eg below is the data:

In "Controloperator" table :

ID Date Completed Type of Control
1 21/09/2010 Control1
2 22/09/2010 Control2
3 23/09/2010 Control3

In "TeamLeader" table :

ID Date Completed Type of Control
1 21/09/2010 Control1
2 22/09/2010 Control2
3 24/09/2010 Control4

So considering the above say 23 rd and 24 th as working day. In table Controloperator 24/09/2010 record is missing and in table Teamleader 23/09/2010 record is missing. So my Result should be as below.

First Msg Box popping up as " Not approved" as there is a mismatch.

Then the result of the query must be as below:

ID Date Completed Type of Control Table Name
3 23/09/2010 Control3 Controloperator
3 24/09/2010 Control4 TeamLeader.

If all records are matching in two tables then it has to pop up message box saying "Approved". Please Help.
Sep 29 '10 #3
MMcCarthy
14,534 Expert Mod 8TB
Does this make any difference?

Expand|Select|Wrap|Line Numbers
  1. SELECT c.ID, c.[Date completed], c.[Type of Control], "Approved" AS Status
  2. FROM Controloperator c INNER JOIN TeamLeader t
  3. ON c.[Type of Control] = t.[Type of Control]
  4. AND c.[Date completed] = t.[Date completed]
  5. AND c.ID = t.ID
  6. UNION
  7. SELECT c.ID, c.[Date completed], c.[Type of Control], "Not Matched" AS Status
  8. FROM Controloperator c LEFT JOIN TeamLeader t
  9. ON c.[Type of Control] = t.[Type of Control]
  10. AND c.[Date completed] = t.[Date completed]
  11. AND c.ID = t.ID
  12. WHERE t.ID Is Null
  13. UNION
  14. SELECT t.ID, t.[Date completed], t.[Type of Control], "Not Matched" AS Status
  15. FROM TeamLeader t LEFT JOIN Controloperator c
  16. ON t.[Type of Control] = c.[Type of Control]
  17. AND t.[Date completed] = c.[Date completed]
  18. AND t.ID = c.ID
  19. WHERE c.ID Is Null
Sep 29 '10 #4
But, I need the table name. Now the result for the above query is :

ID Date Completed Type of Control
3 23/09/2010 Control3
3 24/09/2010 Control4

Nmae of the table is missing. What i want is the below result:

ID Date Completed Type of Control Table Name
3 23/09/2010 Control3 Controloperator
3 24/09/2010 Control4 TeamLeader.
Sep 29 '10 #5
MMcCarthy
14,534 Expert Mod 8TB
This should do it ...

Expand|Select|Wrap|Line Numbers
  1. SELECT c.ID, c.[Date completed], c.[Type of Control], "Approved" AS Status, "Both" AS TableName
  2. FROM Controloperator c INNER JOIN TeamLeader t
  3. ON c.[Type of Control] = t.[Type of Control]
  4. AND c.[Date completed] = t.[Date completed]
  5. AND c.ID = t.ID
  6. UNION
  7. SELECT c.ID, c.[Date completed], c.[Type of Control], "Not Matched" AS Status, "Controloperator" AS TableName
  8. FROM Controloperator c LEFT JOIN TeamLeader t
  9. ON c.[Type of Control] = t.[Type of Control]
  10. AND c.[Date completed] = t.[Date completed]
  11. AND c.ID = t.ID
  12. WHERE t.ID Is Null
  13. UNION
  14. SELECT t.ID, t.[Date completed], t.[Type of Control], "Not Matched" AS Status, "TeamLeader" AS TableName
  15. FROM TeamLeader t LEFT JOIN Controloperator c
  16. ON t.[Type of Control] = c.[Type of Control]
  17. AND t.[Date completed] = c.[Date completed]
  18. AND t.ID = c.ID
  19. WHERE c.ID Is Null
Sep 29 '10 #6
Great Awesome thanks :-)
Sep 29 '10 #7

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

Similar topics

5
by: Arun Wadhawan | last post by:
Hello MY SQL Server is causing me this problem : Microsoft VBScript runtime error '800a000d' Type mismatch: 'ident' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I am getting from...
1
by: Mark | last post by:
Hi - I tried this in VS.Net, and also in the Web Matrix code below: - but I am getting a type mismatch error. The sql statement runs perfectly from within the Access Query Designer. Can anyone...
3
by: Faz | last post by:
Even after changing Invoice_ID to Clng(Invoice_ID) and trying to link to the Reference_2 field which is text it is giving me a type mismatch error. So I created a new query in access where I...
1
by: j.mandala | last post by:
I created a simple link between two tables in a query. TableA has Social Security numbers stored as Long Integer Data. (I imported this table). The Join is between these two fields Table ...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
0
by: news.paradise.net.nz | last post by:
I have been developing access databases for over 5 years. I have a large database and I have struck this problem with it before but can find nothing in help or online. Access 2000 I have a query...
1
by: ArcadeJr | last post by:
Good morning all! I have been getting a Run-time Error message #3464 - Data Type mismatch in criteria expression. While trying to run a query. I have a database where the field Asset_Number...
6
by: rn5a | last post by:
I am inserting records in a MS-Access database table. The data type of one of the columns named *OrderDate* in the DB table is Date/Time. This is the SQL query I am using to insert the records in...
19
by: Lysander | last post by:
I have written a query that takes three integers representing day,month and year, forms a date from them and compares this date to the date the record was entered and returns any records where the...
2
by: padmaneha | last post by:
There are two tables in our database Businessmaster and locationmaster Businessmaster has the following fields like contentid, businessname, businesstype, location, city, state, country, zipcode,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.