Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Good morning,

I am working with two SQL tables, one is a sort of "middle table" that is used to import data from individual offices to a "final table".

What I have been asked to do is to write a query that uses a specific identifying field to find out what are in the "middle table" but are not in the "final table".

The logic would be something like, "If *field* is in 'middle table' and 'final table'=True. If *field* is in 'middle table' but not in 'final table'= False. If True=disregard, if False=display results."

First, is my logic right?

Second, what SQL function would I use to make this happen?
Posted

SQL
SELECT foo
FROM middle_table a LEFT OUTER JOIN final_table b
ON a.foo = b.foo
WHERE b.foo IS NULL


Is an alternative method for getting the values from the middle table not in the final table
 
Share this answer
 
Comments
Herman<T>.Instance 18-Aug-11 11:16am    
In Oracle this was THE trick to do this. Fast and very functional in very large tables, so my 5
Simon_Whale 18-Aug-11 11:17am    
Thanks :) Its also suprisingly quick in SQL Server too
There is no SQL function for this, just code

SQL
SELECT foo
FROM middle_table
WHERE foo NOT IN(SELECT foo FROM final_table)
 
Share this answer
 
Comments
Reiss 18-Aug-11 9:10am    
+5 - nice and simple
I tend to use sql compare/[^] for this kind of task - admittedly it's not free, but it's a darn good bit of kit.

[Disclaimer]I don't and never have worked for Red Gate Software (I don't even knowingly know anyone who does)
 
Share this answer
 
Comments
[no name] 18-Aug-11 9:14am    
A simple SQL statement will do the same thing, with no cost at all. Visual Studio also provides the same functionality.
Reiss 18-Aug-11 9:57am    
true, just another way to do it that's all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900