Solved

opposite of UNION ?

Posted on 2010-09-08
7
1,450 Views
Last Modified: 2012-05-10
I understand how the UNION keyword works, in that it enables one to COMBINE queries into one result set ADDITIVELY.

Is there a way to specify a query, and then TAKE AWAY the results of another query..

Eg.

"give me all schools in the extended programme"

"and then remove all that don't have an annual return done"

(the above is not easily done in one query)

We are using SQL Server 2000

many thanks!!
0
Comment
Question by:zorba111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33626261
INTERSECT
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33626277
with sql 2000, you either use * WHERE NOT EXISTS()* WHERE NOT IN ()* LEFT JOIN ... WHERE  ... IS NULL or a temp table, and do a DELETE based on the second query ...many ways to shave the cat ..
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33626291
Sorry, I didn't read properly, it is EXCEPT. But that only works 2005+

The next best would be NOT IN (subquery) if you have a single column to test on, or LEFT JOIN together with WHERE T2.idcol is null to filter out the matches from the 2nd table.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 13

Expert Comment

by:TechTiger007
ID: 33626352
You can use "minus" to achieve this
eg.
(select * from test) minus (select * from test where result="pass")
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 33626387
Sorry I didnt see that this is about sql server. minus is for oracle
0
 
LVL 2

Expert Comment

by:SrinathKomirishetty
ID: 33626547
Try this

--If Second table contains only Annual schools info.
SELECT * FROM FirstTable
WHERE SchoolIdentifierColumn IN (SELECT SchoolIdentifierColumn FROM SecondTable)

--If Second table contains Annual schools info and other than annaul schools info
SELECT * FROM FirstTable
WHERE SchoolIdentifierColumn NOT IN (SELECT SchoolIdentifierColumn FROM SecondTable WHERE AnnualColumn=0)
0
 

Author Comment

by:zorba111
ID: 33627889
@angell

I did it using
* LEFT JOIN ... WHERE  ... IS NULL
in combination with
* WHERE NOT EXISTS()
and
* WHERE EXISTS()

I also did it with

or a temp table, and do a DELETE based on the second query ...

so as I could check the result set agains the 2nd method.

both tallied (after a bit of tweaking) !
thank you !!
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question