Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

opposite of UNION ?

Posted on 2010-09-08
7
Medium Priority
?
1,617 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
7 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33626261
INTERSECT
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

783 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