Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SELECT NOT IN problem

Posted on 2008-06-11
8
Medium Priority
?
602 Views
Last Modified: 2010-04-21
A query I was using to generate a set of results has suddenly stopped producing results and I don't understand why.

I have 2 tables - Opportunities and Quotes.  Each table is related to a customer table using UID.

I want to produce a set of results showing the customer UIDs for which we have raised quotes but not opportunities.

My query is as follows:

SELECT quotes.uid FROM quotes WHERE quotes.uid NOT IN (SELECT opportunities.uid FROM opportunities);

This was producing results but now it's not.

I've checked the individual tables and there are records that have quotes but not opportunities, so why has this suddenly stopped working?

This is a SQL 2000 database.  I was running reports through an ASP intranet application, but since it stopped working, I've been testing this query in query analyzer.

Please tell me if I'm making a fundamental error or being stupid.  I'm pulling my hair out!

0
Comment
Question by:neburton
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:dragos_craciun
ID: 21757881
well .. you may try

SELECT DISTINCT quotes.uid FROM quotes
MINUS
SELECT DISTINCT opportunities.uid FROM opportunities
0
 
LVL 6

Expert Comment

by:dragos_craciun
ID: 21757884
Or

SELECT quotes.uid FROM quotes
WHERE NOT EXISTS (SELECT 1 FROM opportunities WHERE opportunities.uid = quotes.uid );
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 21757961
My guess about the "why" it worked and now no longer is that in opportunities.uid you have at least 1 row with NULL ...

note that the NOT EXISTS should give better results (assuming proper indexes anyhow)
SELECT quotes.uid FROM quotes WHERE quotes.uid NOT IN (SELECT opportunities.uid FROM opportunities WHERE uid IS NOT NULL);

Open in new window

0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 1

Author Comment

by:neburton
ID: 21757974
is MINUS a T-SQL command??  Tried it in query analyzer and it produced two sets of results.  Can't see anything about it in the SQL Server Books online.  

As for the second query, that seems to work.  I'm just running through the items to make sure.

Any idea why my first query didn't work?
0
 
LVL 1

Author Closing Comment

by:neburton
ID: 31466054
Thanks for this.  Really wanted to get to the bottom of why my original query stopped working and this explained it!  Now I need to figure out what caused the null to creep into my table!
0
 
LVL 6

Expert Comment

by:dragos_craciun
ID: 21757989
MINUS is a relational operator, like UNION
There is also INTERSECT

I'm not sure whether MS chose to implement them or not, and if yes, what exactly is the syntax
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21758019
<Grading Comments>
Thanks for this. Really wanted to get to the bottom of why my original query stopped working and this explained it! Now I need to figure out what caused the null to creep into my table!
</Grading Comments>

glad I could help.
now, a NULL creeping into the tables means you allow NULLS on the column in the first place.
setting the column to NOT NULLable will prevent this from happening again.

in regards to the MINUS, sql server is not implementing this.
btw, I don't like it at all, as it has the same side effects as DISTINCT: it works on the entire row returned, so if one day you add 1 more column to the output you won't get the same results...
0
 
LVL 1

Author Comment

by:neburton
ID: 21758027
Thanks again.    Learn something every day.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

730 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