Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing 2 tables and showing the results 2

Status
Not open for further replies.

miller1975

Programmer
Apr 19, 2005
58
US
Hello All,
I am attempting to do the following.
I have 2 tables, I am going to call it Tbl_Old (has old data) and Tbl_New (has new data).
Both the tables have same field names and structure. I need to compare these 2 tables and show the difference in a new table, which I will call Tbl_Result.

Any ideas on how to do this. Maybe a query to find the difference.
Also I want to know how to move data from one table to another. Again both tables have same field structure.
Please advice.
 
Which kind of difference ?
A starting point for missing records:
SELECT O.*, 'Tbl_Old only' AS Diff
FROM Tbl_Old O LEFT JOIN Tbl_New N ON O.PrimaryKey=N.PrimaryKey
WHERE N.PrimaryKey Is Null
UNION SELECT N.*, 'Tbl_New only'
FROM Tbl_Old O RIGHT JOIN Tbl_New N ON O.PrimaryKey=N.PrimaryKey
WHERE O.PrimaryKey Is Null

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hello PH,
The logic of difference is..as follows.
I will run a query and get data in Tbl_Old, then I will run the same query couple of days later and get the data this time in Tbl_New

By difference I mean, what new records are there in Tbl_New in comparison to Tbl_Old.
There are no primary keys defined at this time. Both tables are same field names.
 
There are no primary keys defined
Really ?
Even no unique index ?
How are your tables updatable without PK ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Updating the tables what I do is..
Run the batch which will first empty the old table.
then move data from tbl_new to tbl_old and then empty tbl_new and i run the query dump the data..thats it..no primary key..

I hope i explained this right..
I also want to know how to move data from Tbl_New to Tbl_Old.
 
How do you differentiate records in tbl_new or tbl_old ?
No field without duplicate value?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
good question.
The scenario is i will run query for tbl_old today and say for example I got 100 records.
Tomorrow I will run the same query again...but this time the data is stored in tbl_new and I got say 90 or 110 records.
In comparison of Tbl_new with Tbl_old we find the records that are new unique records. These new records should be saved to the new table called Tbl_result.

Hope this helps in the explanation..let me know I can give more informaiton if requird.
 
I can give you the field names if you need it also...
 
The real issue is which field(s) identify a record in tbl_old (and thus in tbl_new) without ambiguity.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Is it not possible to check the entire record in one table to entire record in the other table.
 
check the entire record in one table to entire record in the other table
You really insist following the hard way ?
SELECT O.*, 'Tbl_Old only' AS Diff
FROM Tbl_Old O LEFT JOIN Tbl_New N
ON O.Field1=N.Field1 AND O.Field2=N.Field2 ... AND O.FieldX=N.FieldX
WHERE N.Field1 Is Null
UNION SELECT N.*, 'Tbl_New only'
FROM Tbl_Old O RIGHT JOIN Tbl_New N
ON O.Field1=N.Field1 AND O.Field2=N.Field2 ... AND O.FieldX=N.FieldX
WHERE O.Field1 Is Null

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I am sorry but I am not trying to do it the hard way..but I cant think of any other way to get the difference between 2 tables.
can you suggest any other method...
 
PHV said:
The real issue is which field(s) identify a record in tbl_old (and thus in tbl_new) without ambiguity

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hello PH,
Very hard to tell which field(s) identify a record in tbl_old (and thus in tbl_new) without ambiguity as like I said both tables have almost same data except for some new ones that might have got added during the day..so basically we are trying to just find the new records that are there in Tbl_New in comparison to Tbl_Old.
This is the best explanation...
 
This is the best explanation
I disagree.
The real issue is which field(s) identify a record in tbl_old without ambiguity vs the another records in tbl_old.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
So if you were to do a query in a similar situation how will u do it..
I am sorry if i am not explaining things right..
 
the query you have given seems to be working..but it gives one extra field called "Diff"..how can I not show up..
 
I tried it and I made it as a make table query to get the difference records..and it looks good..
but the new table also shows the different records in the old table also.
the new table should be looking at the old one as master table and only the difference from new table should be shown.
 
I fixed tht ....I changed the line to this so now it only show the new reocrds in Tbl_New
Code:
SELECT O.*, 'Tbl_New only' AS Diff

But it saves it as a Cross Tab query..I cant save it as a Make Table Query...help...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top