473,396 Members | 2,115 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.

Comparing Two Similar Tables for Unmatched Values in Multiple Columns

I have two tables with exactly the same column fields and I'd like to compare the values across about 15 columns from TableA to TableB to determine which Primary ID's have values in one table that do not match the second. In other words I'd like to see if columns 2-15 for a Primary ID in TableA match columns 2-15 for the same Primary ID in TableB. All values in the columns to be compared are numeric. I'd like a solution that would return any and all discrepancies. Please let me know if I need to explain further.
Aug 13 '14 #1

✓ answered by twinnyfo

Try something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT TableA.PrimaryKey,
  2.        IIf(TableA.Field1<>TableB.Field1,"X","") AS Field1Check,
  3.        IIf(TableA.Field2<>TableB.Field2,"X","") AS Field2Check,
  4.        IIf(TableA.Field3<>TableB.Field3,"X","") AS Field3Check,
  5.        . . .
  6. FROM TableA INNER JOIN TableB
  7.        ON TableA.PrimaryKey = TableB.PrimaryKey;
This should create a grid with an X in every field that differs between tables.

Not the simplest, and probably not the best solution, but just the first thing that popped to mind. This would allow you to check the values of the fields.

10 1523
twinnyfo
3,653 Expert Mod 2GB
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT TableA.PrimaryKey,
  2.        IIf(TableA.Field1<>TableB.Field1,"X","") AS Field1Check,
  3.        IIf(TableA.Field2<>TableB.Field2,"X","") AS Field2Check,
  4.        IIf(TableA.Field3<>TableB.Field3,"X","") AS Field3Check,
  5.        . . .
  6. FROM TableA INNER JOIN TableB
  7.        ON TableA.PrimaryKey = TableB.PrimaryKey;
This should create a grid with an X in every field that differs between tables.

Not the simplest, and probably not the best solution, but just the first thing that popped to mind. This would allow you to check the values of the fields.
Aug 13 '14 #2
This looks great, however, I'm getting an error "compile error: expected: case". I'm still extremely new to coding and have only been able to edit and tweak other codes. Is there something I'm missing before or after the code you provided? The bold sections are where there are errors. I sincerely appreciate your help!!



SELECT TableA.PrimaryKey,

IIf(TableA.Field1<>TableB.Field1,"X","") AS Field1Check,
IIf(TableA.Field2<>TableB.Field2,"X","") AS Field2Check,
IIf(TableA.Field3<>TableB.Field3,"X","") AS Field3Check
FROM TableA INNER JOIN TableB
ON TableA.PrimaryKey = TableB.PrimaryKey;
Aug 13 '14 #3
twinnyfo
3,653 Expert Mod 2GB
This is merely a SQL statement and should not be in a VBA module. This is to buld a query--not VBA code.
Aug 13 '14 #4
Thanks! I feel a bit silly now but at least I'm learning. I really appreciate your help!!!
Aug 13 '14 #5
twinnyfo
3,653 Expert Mod 2GB
Glad I could help. Let us know if you come across any more challenges.
Aug 13 '14 #6
NeoPa
32,556 Expert Mod 16PB
Fine SQL Twinnyfo. I would just suggest a minor amendment to ensure Null values are handled :
Expand|Select|Wrap|Line Numbers
  1. SELECT TableA.PrimaryKey,
  2.        IIf(Nz(TableA.Field1,'')<>Nz(TableB.Field1,''),'X','') AS Field1Check,
  3.        IIf(Nz(TableA.Field2,'')<>Nz(TableB.Field2,''),'X','') AS Field2Check,
  4.        IIf(Nz(TableA.Field3,'')<>Nz(TableB.Field3,''),'X','') AS Field3Check,
  5.        ...
  6. FROM   TableA
  7.        INNER JOIN
  8.        TableB
  9.   ON   TableA.PrimaryKey=TableB.PrimaryKey
Clearly, if any of the fields are numerc then the Nz() call can use a 0 instead of the '' empty string.
Aug 14 '14 #7
twinnyfo
3,653 Expert Mod 2GB
True, that NeoPa. I seem to forget that all the time in initial coding until it doesn't work because of a null value....

Good catch!
Aug 14 '14 #8
NeoPa
32,556 Expert Mod 16PB
LOL!!

Absolutely understand you there Twinnyfo :-D

With more experience comes the dawning of catching it a little earlier in the process. Mainly work here has given me that experience if I'm honest :-)
Aug 15 '14 #9
Thanks!! My query is running smoothly now! Appreciate all your efforts :)
Aug 15 '14 #10
twinnyfo
3,653 Expert Mod 2GB
I'm glad we could be of some help. Hope you have a great weekend!
Aug 15 '14 #11

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

Similar topics

16
by: lostinspace | last post by:
Is it possible? TIA
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
4
by: Cezar | last post by:
Hi, I need to display the content of an ArrayList on multiple columns, like data1 data2 data3 data4 data5 data6 data7 data8 ---- or data1 data3 data5 data7
4
by: Brandon Potter | last post by:
I think I'm going crazy, but I cannot remember how to create a listbox with multiple columns as well as the sortable header block (a la Outlook message list). I've seen the examples of how to...
0
by: machanicks1275 | last post by:
Hi! I was just wondering if it was possible, in SQL, for a CASE expression to return multiple columns (a row)...? I know you can't do in this in a SELECT caluse, but is there anywhere else you CAN...
2
by: ray well | last post by:
i need to display 2 columns of data in a list box. how would i set this up IN CODE. say my table is tblNames, and i have 2 fields, FirstName, LastName, and want the data to show up in 2...
1
by: Hetal | last post by:
Hi... We are on Visual Basic .NET 2003 and i am using DataView to sort the DataTable. However, we are facing problems with sorting the data based on multiple columns. The below statement to...
3
by: Sunny | last post by:
Hello, The problem is that I want to select distinct values from multiple columns of one table and want to select one column. Say column1 has values: "first","second","third" and column2 has...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.