neburton
asked on
SELECT NOT IN problem
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!
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!
Or
SELECT quotes.uid FROM quotes
WHERE NOT EXISTS (SELECT 1 FROM opportunities WHERE opportunities.uid = quotes.uid );
SELECT quotes.uid FROM quotes
WHERE NOT EXISTS (SELECT 1 FROM opportunities WHERE opportunities.uid = quotes.uid );
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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?
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?
ASKER
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!
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
There is also INTERSECT
I'm not sure whether MS chose to implement them or not, and if yes, what exactly is the syntax
<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...
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...
ASKER
Thanks again. Learn something every day.
SELECT DISTINCT quotes.uid FROM quotes
MINUS
SELECT DISTINCT opportunities.uid FROM opportunities