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

Compute table difference

Hi All,

I have to tables with an identical structure. I want to compute the difference between this tables (probably by using some SQL statement).

Question is, how to do so?

Layout of the tables: (SomeID, Name, Date). Primary key: the whole structure (no, SomeID can't be used for key, autonumber field will not work either).

I'd be glad to use an EXCEPT of MINUS construction, but these seem to be unsupported in Access.

I'd be happy to receive any advice..

Best, Robin
Dec 17 '07 #1
3 1263
FishVal
2,653 Expert 2GB
Hi, Robin.

The following query unions records form [tbl1] not matching any record in [tbl2] with records form [tbl2] not matching any record in [tbl1]. To find unmatched record an outer join is used. Nz() function is used to resolve joins on fields containing Null.

Expand|Select|Wrap|Line Numbers
  1. SELECT tbl1.SomeID, tbl1.Name, tbl1.Date FROM tbl1 LEFT JOIN tbl2 ON Nz(tbl1.SomeID)=Nz(tbl2.SomeID) AND Nz(tbl1.Name)=Nz(tbl2.Name) AND Nz(tbl1.Date)=Nz(tbl2.Date) WHERE tbl2.SomeID Is Null AND tbl2.Name Is Null AND tbl2.Date Is Null
  2. UNION
  3. SELECT tbl2.SomeID, tbl2.Name, tbl2.Date FROM tbl2 LEFT JOIN tbl1 ON Nz(tbl1.SomeID)=Nz(tbl2.SomeID) AND Nz(tbl1.Name)=Nz(tbl2.Name) AND Nz(tbl1.Date)=Nz(tbl2.Date) WHERE tbl1.SomeID Is Null AND tbl1.Name Is Null AND tbl1.Date Is Null;
  4.  
Dec 17 '07 #2
Hi there Fishval,

First of all I'd like to thank you for the nice and extensive comment.

Sadly enough, I cannot it to work entirely. I've replaced (textual replace) some values in your query. When I execute that Query, I get the results I want to have. That's good.

However, as soon as I make this query a subquery of an "INSERT INTO" statement, Access returns an error (Syntax ERROR in INSERT statement).

So this works:
Expand|Select|Wrap|Line Numbers
  1. SELECT WerknemersAllDates.WerknID, WerknemersAllDates.Achternaam, WerknemersAllDates.Date FROM WerknemersAllDates LEFT JOIN WerknemersPlannedDates ON Nz(WerknemersAllDates.WerknID)=Nz(WerknemersPlannedDates.WerknID) AND Nz(WerknemersAllDates.Achternaam)=Nz(WerknemersPlannedDates.Achternaam) AND Nz(WerknemersAllDates.Date)=Nz(WerknemersPlannedDates.Date) WHERE WerknemersPlannedDates.WerknID IS NULL AND WerknemersPlannedDates.Achternaam IS NULL AND WerknemersPlannedDates.Date IS NULL 
  2. UNION SELECT WerknemersPlannedDates.WerknID, WerknemersPlannedDates.Achternaam, WerknemersPlannedDates.Date FROM WerknemersPlannedDates LEFT JOIN WerknemersAllDates ON Nz(WerknemersAllDates.WerknID)=Nz(WerknemersPlannedDates.WerknID) AND Nz(WerknemersAllDates.Achternaam)=Nz(WerknemersPlannedDates.Achternaam) AND Nz(WerknemersAllDates.Date)=Nz(WerknemersPlannedDates.Date) WHERE WerknemersAllDates.WerknID IS NULL AND WerknemersAllDates.Achternaam IS NULL AND WerknemersAllDates.Date IS NULL;
  3.  
However, this doesn't:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO WerknemersAvailableDates ([WerknID], [Achternaam], [Date]) SELECT WerknemersAllDates.WerknID, WerknemersAllDates.Achternaam, WerknemersAllDates.Date FROM WerknemersAllDates LEFT JOIN WerknemersPlannedDates ON Nz(WerknemersAllDates.WerknID)=Nz(WerknemersPlannedDates.WerknID) AND Nz(WerknemersAllDates.Achternaam)=Nz(WerknemersPlannedDates.Achternaam) AND Nz(WerknemersAllDates.Date)=Nz(WerknemersPlannedDates.Date) WHERE WerknemersPlannedDates.WerknID IS NULL AND WerknemersPlannedDates.Achternaam IS NULL AND WerknemersPlannedDates.Date IS NULL 
  2. UNION SELECT WerknemersPlannedDates.WerknID, WerknemersPlannedDates.Achternaam, WerknemersPlannedDates.Date FROM WerknemersPlannedDates LEFT JOIN WerknemersAllDates ON Nz(WerknemersAllDates.WerknID)=Nz(WerknemersPlannedDates.WerknID) AND Nz(WerknemersAllDates.Achternaam)=Nz(WerknemersPlannedDates.Achternaam) AND Nz(WerknemersAllDates.Date)=Nz(WerknemersPlannedDates.Date) WHERE WerknemersAllDates.WerknID IS NULL AND WerknemersAllDates.Achternaam IS NULL AND WerknemersAllDates.Date IS NULL;
  3.  
I've tried to play a bit with brackets (to revent scoping errors), however, that doesn't do the trick (or I'm doing it wrong).

Could you perhaps tell me what's going wrong?

Thanks again, Robin
Dec 19 '07 #3
FishVal
2,653 Expert 2GB
Hi, Robin.

Try to simlify expression.
Save the first query - let us say it will be [qryAllvsPlannedDiff].
Then use it as source dataset in the second query
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO WerknemersAvailableDates ([WerknID], [Achternaam], [Date]) SELECT qryAllvsPlannedDiff.WerknID, qryAllvsPlannedDiff.Achternaam, qryAllvsPlannedDiff.[Date] FROM qryAllvsPlannedDiff;
  2.  
Dec 20 '07 #4

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

Similar topics

61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
2
by: Michael Howes | last post by:
I have a single DataTable in a DataSet. It has 4 columns and i'd like to get a handful of counts of unique items in 3 of the 4 columns. Can a DataTables Select or Compute methods to COUNT DISTINCT?...
1
by: skirkby | last post by:
This will be obvious to some - but not me I'm afraid... I am using an SQL data link from my ASP application to a SPROC - this all works fine on standard SELECT statements and JOIN in to a...
2
by: Bubb | last post by:
I have an Access database with one table that I use for stuff I sell online. Each record has the following fields: Unique Id, Cost, and Item Description. I just obtained some more stuff with its...
22
by: MLH | last post by:
If 3 things can be in one of 2 states, the number of possible combinations is equal to 2^3. But if I have 3 things, 2 of which can be in 2 states and the other in 3 states, what's the simplest...
1
by: sharathragunathan | last post by:
HI, I have an Access novice and I have a question.I have a form that has a request date that a user enters. What I need is when you click on a command button, say Elapsed Time, it should give...
0
by: ccarter45 | last post by:
I need to compute the differences in color between 2 pictures. Also, I need to write a method that computes a useful difference color of two colors (where the colors and their difference will be...
6
by: W. eWatson | last post by:
That's the question in Subject. For example, the difference between 08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and 03/03/2008 is 4, leap year--extra day in Feb. I'm really...
1
by: iceman23 | last post by:
Hello, My question in teradata would be, will it be possible to get the time difference of 2 given date/time, given that the first value of the would be the 1st row of 2nd column then the second...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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...

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.