473,471 Members | 4,637 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Compare 2 tables to find out which rows were deleted, added, or changed

3 New Member
I need to create a delta table that shows me the differences between two tables (I need to know which rows have been added, which rows have been deleted, and which rows have changed from the old table to the new table). The old and new table have 5 fields with the exact same information. I do not have a primary key in my access database that links the two tables because many of the rows have duplicates in 2 or more fields.

Mind you, I am very new to access and this example below may not be the best example or the best way for what I want to be represented.

Example:
Table1 ("Old")
Field1, Field2, Field3
XX, VV, H
XX, VV, P
YY, GG, N
ZZ, GG, N

Table2 ("New")
Field1, Field2, Field3
XX, VV, S
XX, VV, P
YY, GG, N
HH, FF, C

What I want the query to show

Field1, Field2, Field3, Changes
S Field3 Updated
GB F Field Updated
Row 3 Removed
Row 4 Added



This is what I have done so far.

Expand|Select|Wrap|Line Numbers
  1. SELECT Old.*, New.*,
  2. "Field Updated" As Comment
  3. FROM Old
  4.     inner JOIN New
  5.          ON Old.Field1 = New.Field1 AND Old.Field2= New.field2 and Old.field3=New.field4
  6. union
  7.  
  8. SELECT  Old.*, New.*,
  9.  "No Change" As Comment
  10. FROM Old INNER JOIN New ON (Old.Field1 = New.field1) AND (Old.field2 = New.field2) AND (Old.field3 = New.field3) AND (Old.field4= New.field4) AND (Old.field5 = New.field5);
  11.  
  12.  
  13. Union
  14.  
  15. SELECT  Old.*, New.*, 
  16. "Deleted" as Comment
  17. FROM Old LEFT JOIN New ON (Old.field1 = New.field1) AND (Old.field2 = New.field2) AND (Old.field3 = New.field3) AND (Old.[field4] = New.[Field4])
  18. WHERE (((New.Field1) Is Null) AND ((New.Field2) Is Null) AND ((New.Field3) Is Null) AND ((New.field4) Is Null));
  19.  
  20. UNION 
  21. SELECT  Old.*, New.*, "Added" as Comment
  22.  
  23. FROM New LEFT JOIN Old ON (New.field1 = Old.field1) AND (New.field2 = Old.field2) AND (New.field3 = Old.field3) AND (New.field4 = Old.field4) AND (New.[field5] = Old.[field5])
  24. WHERE (((Old.field1) Is Null) AND ((Old.field2) Is Null) AND ((Old.field3) Is Null) AND ((Old.field4) Is Null) AND ((Old.field5) Is Null));


Please I am in desperate need of any advice and suggestions. Please be as specific as possible with your answers since I am knew to this!! Thanks
Jan 14 '13 #1
1 1784
NeoPa
32,556 Recognized Expert Moderator MVP
I see this question as borderline acceptable. It is almost a simple request for the work to be done, but there is some work already done, albeit very confused looking and a long way short of practical. Nevertheless, effort has been shown and work has been done already so I'm inclined to go ahead. My approach will be to attempt to lead towards a solution rather than simply to provide one.

To this end I would ask you first to consider (and then explain to us) what should actually be used to identify records from the two tables as matches. What you suggest (That all five fields contain the same values) is inconsistent with the idea of showing where the same records exist but have been changed (As that would make no sense in the scenario described).

When we have a clear idea of exactly what we're looking for I will ask you to do your best to produce each of the queries that go together to make up the whole solution. That way we will be breaking down the problem into manageable chunks. It will also force you to focus your understanding on each of the situations separately, before we bring them all together. A task that will make better sense to you once you've been through each individually.
Jan 15 '13 #2

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

Similar topics

3
by: Paolo Alexis Falcone | last post by:
Whenever I try to access a table with many rows using PgSQL's fetchall(), this happens: >>> from pyPgSQL import PgSQL >>> db = PgSQL.connect("192.168.0.8:5432:whitegold","dondon","dondon") >>>...
4
by: Support | last post by:
Hi, I want to know if I have changed a few records in my database using update / insert / delete methods, how can i later know which rows have been changed or modified ? I know the...
1
by: anthonyroach | last post by:
I can create a dumpfile to see the rows rejected by the load. How can I see the rows deleted in the delete phase?
9
by: Plissken.s | last post by:
Hi, how can i compare a string which is non null and empty? i look thru the string methods here, but cant find one which does it? ...
9
by: rochelle28 | last post by:
i want to compare the two consecutive rows in the table such that if the first row contains handset in the category column and the next row contins simcard in category column it will alert the user,...
7
by: jb1 | last post by:
Hello All, I am trying to create a DTS package. I have two tables tbl_A and tbl_B with similar data/rows but no primary keys. tbl_A is master. I would like this package to query tbl_A and...
2
by: Sam Durai | last post by:
Whenever delete is performed on a DB2 database, it just returns "...command completed successfully.." db2 =select count(*) from sales 1 ----------- 12 1 record(s) selected. db2 =delete from...
7
by: Deano | last post by:
Got a bit of a major task coming up - I need to migrate data from one version of my mdb to another. Normally I manually track what's changed but there have been so many alterations it's been...
2
by: site | last post by:
Have to compare dates of two rows in one table Input table COL1| COL2| COL3| COL4 1| A| 1-Jan-07| 6-Feb-08 2| A| 7-Feb-08| 31-Mar-08 3| A| 1-Jan-09| NULL SQL code to compare COL 4 date (Row...
1
by: evildracko | last post by:
first a write error prompt Title: Write Conflict Text: This record has been changed by another user since you started editing it. If you save the record, you will overwrite the changes the other...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.