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

intersect & except

144 100+
hi,

how to find intersections and difference on two-sets operations?
can i use the intersect and except keyword?


thank you very much
Mar 6 '08 #1
1 1161
Hi Thesti

I am assuming you want to work this out in SQL Server and not .NET...

Here is the approach I would take:
Assuming I had TableA and TableB. I want to find out all the records in TableA that aren't in TableB, based on a column RecordId. First step, find out all the records in TableA that ARE in TableB:

Expand|Select|Wrap|Line Numbers
  1. SELECT a.RecordId
  2. FROM TableA a
  3. INNER JOIN TableB b
  4. ON a.RecordId = b.RecordId
  5.  
Then, expand the code so that you return the records that AREN'T in this resultset:

Expand|Select|Wrap|Line Numbers
  1. SELECT TableA.*
  2. FROM TableA
  3. WHERE TableA.RecordId NOT IN
  4. (
  5. SELECT a.RecordId
  6. FROM TableA a
  7. INNER JOIN TableB b
  8. ON a.RecordId = b.RecordId
  9. )
  10.  
This can also be written as a OUTER JOIN with a WHERE TableB.SomeColumn IS NULL. A query written this way will also run faster, but I find it harder to read, so I didn't write it that way.

HTH
Mar 7 '08 #2

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

Similar topics

6
by: Nick | last post by:
I have three tables books (id, title, author) authors (id, name) authors_groups (author_id, group_id) I have a query that searches for authors who must be in a least one subgroup of two...
1
by: sunaina | last post by:
I am doing the following query using intersect but gives me an error 'error in sql syntax. I tried using join as well but gives me similar error. In the following code still has intersect just to...
1
by: Brian | last post by:
Greetings all, I wrote a message regarding hit-lines here a couple of days back saying: "Anyone got links to some examples of using Region and/or Region.Intersect in a graphical / gaming...
5
by: James Foreman | last post by:
Hi, I'm coming back to Sql Server after 4 years away, using other RDBMS, and there's a few things I'm struggling to remember how to do (if I could do them in the first place...) Main amongst...
2
by: ngenius | last post by:
Hello everyone, I am new to MySQL but just realized it does not support INTERSECT in MySQL 5.0 Community Server. I have this problem to solve. (1) cust_table cust_id city_code. ...
1
by: Xpertboy | last post by:
i am trying to execute following code in asp var strSQL1="select Model from Phones where Brand='"+ brand +"'"; var strSQL2="select Model from Phones where Model='"+ model +"'"; var...
3
by: tmuldner | last post by:
Does anybody know any implementation of XPaths that supports path intersection and difference?
3
by: bzb | last post by:
hi ALL, I have a situation to use INTERSECT in my query, but INTERSECT is not supported in "mysql 4.1.15". Can anyone explain me how to tackle this problem..... ! Also, is there...
0
by: egur | last post by:
Hi, I'm looking for reliable MySQL version (for Windows) that supports "EXCEPT" and "INTERSECT" execution. Would somebody recommend such version (and corresponding client), please?
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.