Solved

opposite of UNION ?

Posted on 2010-09-08
7
1,234 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
back end of ssas cube views or tables? 2 24
Analysis of table use 7 27
t-sql month question 8 42
Sort by Month and Year - SQL 3 22
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

708 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

14 Experts available now in Live!

Get 1:1 Help Now