473,395 Members | 1,530 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,395 software developers and data experts.

Efficient way to compare data in 3 tables and only show the discrepancy

Is there a way to triple check that the data is consistent between the three tables and show if there are any discrepancies in any of the three tables? Each table is not completely the same but they all have an EmployeeID, amount, datepaid, and CID column. When I did with two tables I thought I had the SQL as:

Expand|Select|Wrap|Line Numbers
  1. SELECT tbMaster.EmployeeID, tbMaster.Amount, tbMaster.DatePaid, tbMaster.CID
  2. FROM tbMaster LEFT JOIN tbCID ON (tbMaster.CID = tbSUID.CID) AND (tbMaster.Amount = tbCID.Amount) AND (tbMaster.DatePaid = tbCID.DatePaid)
  3. WHERE (((tbMaster.[Advance/paid])="Paid Respondent") AND ((tbCID.CID) Is Null));
It worked but if the tbCID had any discrepancies it did not catch it...

So for the three tables I thought:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbMaster.EmployeeID, tbMaster.Amount, tbMaster.DatePaid, tbMaster.[Advance/paid], tbMaster.CID
  2. FROM tbMaster, tbCID, table3
  3. WHERE tbMaster.CID <> tbCID.CID
  4. OR  table3.CID <> tbMaster.CID
  5. OR  table3.CID <> tbCID.CID
  6. OR tbMaster.Amount <> tbCID.Amount
  7. OR  table3.AMOUNT <> tbMaster.AMOUNT
  8. OR  table3.AMOUNT <> tbCID.AMOUNT
  9. OR tbMaster.Datepaid <> tbCID.Datepaid
  10. OR  table3.DATEPAID <> tbMaster.DATEPAID
  11. OR  table3.DATEPAID <> tbCID.DATEPAID
  12.  
But there is only 30 entries yet I get 5x or 6x copies and get over 3000 rows/entries in the query...
Mar 8 '19 #1
7 1112
twinnyfo
3,653 Expert Mod 2GB
BA,

You would NEVER want to create tables like that. You should never have three tables that have duplicate entries like that. All you need is the one table that has that information and have the EmployeeID be the foreign key to that table from any other tables.

As you have demonstrated, this is the reason you don’t duplicate values, because now you have to determine if there are any discrepancies.
Mar 9 '19 #2
Thank you for your response twinnyfo. I agree I would rather just handle one table. Unfortunately I do not control the tables being added and was informed they wanted to triple check the amount, date, and ID so have three tables where they will ask the employees to fill 3x...
I guess I can append query all three tables into one table then do an unmatch query?
Mar 11 '19 #3
twinnyfo
3,653 Expert Mod 2GB
1. You are working on the DB, correct? How come you do not control the tables?

2. If the data is in the tables once, there is no need to triple check any data.

3. If I was an employee I would be asking, "Why do I have to enter this three times? It's the same data!"

4. If you are running an append query, with the same data across three tables, again, there is no need to either triple check or run an "unmatch" query--whatever that may be, becauase the data is (should be) the same.
Mar 11 '19 #4
Hi twinnyfo
I am just someone who makes the frame and am told what to try to make it do. The tables are one for before they pay, when they pay, and a table of receipts to check if what they say they will pay/have paid is what the receipt says? if that makes sense. Yes we are hoping that the data should be the same but I guess there were instances where someone fat fingered an extra digit and got paid extra.
Mar 11 '19 #5
I think I got it!
Expand|Select|Wrap|Line Numbers
  1. SELECT tbMaster.EmployeeID, tbMaster.Amount, tbMaster.DatePaid, tbMaster.[Advance/paid], tbMaster.CID
  2. FROM (tbMaster LEFT JOIN tbCID ON (tbMaster.DatePaid = tbCID.DatePaid) AND (tbMaster.Amount = tbCID.Amount) AND (tbMaster.CID = tbCID.CID)) LEFT JOIN table3 ON (tbMaster.CID = table3.CID) AND (tbMaster.DatePaid = table3.DatePaid) AND (tbMaster.Amount = table3.Amount)
  3. WHERE (((tbMaster.[Advance/paid])="Paid Respondent") AND ((tbCID.CID) Is Null)) OR (((tbMaster.[Advance/paid])="Paid Respondent") AND ((table3.CID) Is Null));
  4.  
Thank you!
Mar 11 '19 #6
twinnyfo
3,653 Expert Mod 2GB
Have fun with this. As your records and requirements expand, this structure will become your primary headache. I don't tell my bosses how to set employee policy. They don't try to tell me how to design the structure of my tables.
Mar 11 '19 #7
Rabbit
12,516 Expert Mod 8TB
Be careful here, that query might not do what you think it does. That query only tells you if there are paid respondent records in the master table that does not exist in table CID or table 3. It does not check the reverse of that situation and it does not check table CID against table 3.

With 3 tables, there are 6 scenarios for missing records, your query only checks 2 of those 6.
Mar 11 '19 #8

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

Similar topics

10
by: xixi | last post by:
we are using db2 udb v8.1 on windows, i would like to know whether we have tool or way to compare two tables on same database for data difference. thanks. (same ddl, different data, try to compare...
2
by: Rachel Curran | last post by:
Please can anybody help me with the following: I have two separate excel spreadsheets that I have imported into access, each sheet holds the same fields. Both spreadsheets hold all information...
1
by: Rachel Curran | last post by:
Please can anybody help me with the following: I have two separate excel spreadsheets that I have imported into access, each sheet holds the same fields. Both spreadsheets hold all information...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
8
sgood
by: sgood | last post by:
A little background first. I am currently working with data from two seperate applications. Both have been exported to CSV files, then queried into their own tables in the access database. The...
1
by: jswag789 | last post by:
I have a database which consists of tblMaster, tblMasterReplica, frmMaster (to display info from tblMaster), frmChange (to make changes to data, which are saved to tblMasterReplica). I also have a...
5
by: anjalive | last post by:
Hello, Can we compare two tables in perl? I mean How can we do it for database tables. I would like to know the solution for below scenario in perl script. Here are 2 tables: Table 1...
11
by: gadrummer | last post by:
I have two tables. One table is 2016 and the other table is rates. I have 3 fields that match in both tables and . What I would like to do is count the number records that the in the 2016 table...
8
by: apopa | last post by:
I have two tables with a unique field which i use to join the tables. I'm trying to compare the tables using access queries and some vba to find the changes within the records from the two tables....
0
by: ahmedsa | last post by:
Specific cast is not valid in linq query when compare two tables Problem Error display in linq query "specific cast is not valid" at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.