Solved

opposite of UNION ?

Posted on 2010-09-08
7
1,257 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 142

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now