Advertisement

04.16.2008 at 07:47AM PDT, ID: 23327302
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Simple Subquery not working

Tags: Microsoft, SQL, 2000
Sorry its either too early in the morning or i have left my brain at home - I just need to be put out of my misery.

I have two tables (which I have simplified but still same idea).  One has all the details of the staff with their extension and supervisor name.  I have another table which will list the supervisors, their extension and their managers.  So at the moment I two tables:


StaffDetails Table.....

StaffName   Supervisor  Ext
Bob         Gary        123
Mary        Gary        112
Jane        Mark        111
Peter       Tony        101
Sandy     Tony        101


StaffSupervisors Table.....

Supervisor  Ext    Manager
Gary        501    Steve
Mark        502    Alan

As you can see in the StaffDetails table, there are staff members Peter and Sandy who have Tony as their supervisor.  But the details of Tony havent been put into the StaffSupervisors table - yet.

So as not to duplicate entries into the Supervisor table, I thought of using the coding in the code snippet below.

However - it returns zero results. If i run the select alone it returns zero results.  If it change the query to look for "IN" instead of "NOT IN" it returns "Gary" and "Mark" as expected.

Please tell me I am stupid and I am missing the obvious, I have done thousands of queries like this and I am stumped as to why this isnt working at my new job.

1:
2:
3:
4:
insert into staffsupervisors (supervisor)
select distinct supervisor 
  from staffdetails
 where supervisor not in (select supervisor from staffsupervisors)
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: Wedmore
Solution Provided By: ScottPletcher
Participating Experts: 4
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
04.16.2008 at 07:56AM PDT, ID: 21368481

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 08:05AM PDT, ID: 21368581

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 08:07AM PDT, ID: 21368603

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 08:23AM PDT, ID: 21368783

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 08:29AM PDT, ID: 21368851

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 08:36AM PDT, ID: 21368939

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 08:43AM PDT, ID: 21369015

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 09:45AM PDT, ID: 21369714

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 09:49AM PDT, ID: 21369748

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 10:35AM PDT, ID: 21370123

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 10:42AM PDT, ID: 21370176

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 11:44AM PDT, ID: 21370742

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 12:34PM PDT, ID: 21371196

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 01:02PM PDT, ID: 21371430

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
04.16.2008 at 07:56AM PDT, ID: 21368481
Try different aproach:
insert into staffsupervisors (supervisor)
select distinct sd.supervisor
  from staffdetails sd
  left join staffsupervisors ss on ss.supervisor=sd.supervisor
where ss.supervisor is null
 
04.16.2008 at 08:05AM PDT, ID: 21368581
I know that works, I am more concerned as to why the NOT IN wont work.  There has to be a reason.
 
04.16.2008 at 08:07AM PDT, ID: 21368603

Rank: Wizard

hi, try if this works

insert into staffsupervisors (supervisor)
select distinct supervisor
  from staffdetails
 where staffdetails.supervisor not in (select supervisor from staffsupervisors)
 
04.16.2008 at 08:23AM PDT, ID: 21368783
Nope - still zero results.

If i run the subquery "select supervisor from staffsupervisors" alone it does pull the list of supervisors in the table.

I even ran an update to remove any spaces which might cause an issue....grrr, i'm not the only one that thinks this is weird right?  

update staffdetails
   set supervisor = rtrim(ltrim(supervisor))


 
04.16.2008 at 08:29AM PDT, ID: 21368851

Rank: Wizard

Does this return any records?

select distinct supervisor
  from staffdetails
 where staffdetails.supervisor not in (select supervisor from staffsupervisors)
 
04.16.2008 at 08:36AM PDT, ID: 21368939
Nope - nothing.  

And again - if i change it from "NOT IN" to "IN", it will return the correct number of records.
 
04.16.2008 at 08:43AM PDT, ID: 21369015
Just to recap....

This works:
select distinct sd.supervisor
  from staffdetails sd
  left join staffsupervisors ss on ss.supervisor=sd.supervisor
 where ss.supervisor is null

This doesnt work:
select distinct supervisor
  from staffdetails
 where staffdetails.supervisor not in (select supervisor from staffsupervisors)

I know I know I know.  I could use the first query, but I have a habit of using subqueries from time to time, so if something so simple isnt working, I'd like to know now rather than a large join with multiple tables etc.
 
04.16.2008 at 09:45AM PDT, ID: 21369714
what about
select distinct supervisor
  from staffdetails
 where ltrim(rtrim(supervisor)) not in (select ltrim(rtrim(supervisor)) from staffsupervisors)
 
04.16.2008 at 09:49AM PDT, ID: 21369748

Rank: Wizard

I tried the query and it is returning Tony.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
create table StaffDetails (Supervisor varchar(100))
create table StaffSupervisors (Supervisor varchar(100))
 
insert into staffdetails values ('Gary')
insert into staffdetails values ('Gary')
insert into staffdetails values ('Mark')
insert into staffdetails values ('Tony')
insert into staffdetails values ('Tony')
 
insert into staffsupervisors values ('Gary')  
insert into staffsupervisors values ('Mark')  
 
select distinct supervisor
  from staffdetails
 where staffdetails.supervisor not in (select supervisor from staffsupervisors)
 
drop table StaffDetails
drop table StaffSupervisors
Open in New Window
 
04.16.2008 at 10:35AM PDT, ID: 21370123
That makes me wonder if there is something about the SQL Server setup we have here.   Lets face it, if the left outer join works, there must be a reason for the subquery not to work.

I also just found something else crazy in another table....

SELECT Manager  FROM StaffManagers WHERE Manager LIKE 'L%'

All of the names returned started with L except one of the rows was "G.Lansky"

I wasnt aware that the wildcard would also be affective after the fullstop.

Note: Not a new or separate question, my original question still stands - why does the subquery work when the left outer join doesnt.
 
04.16.2008 at 10:42AM PDT, ID: 21370176

Rank: Wizard

>All of the names returned started with L except one of the rows was "G.Lansky"
Why are you expecting "G.Lansky" to be returned? It starts with G

>why does the subquery work when the left outer join doesnt.
I think you meant "left outer join works but subquery doesn't". Have you tried the code I posted? Is it returning any records?
 
04.16.2008 at 11:44AM PDT, ID: 21370742
>>Why are you expecting "G.Lansky" to be returned? It starts with G
Thats what I mean - i dont expect it, I dont know why it is being returned.  My only assumption is that its because of the fullstop.

>> "left outer join works but subquery doesn't". Have you tried the code I posted? Is it returning any records?

Correct sorry about that.  Yes I tried it, even copied and pasted, and it returns zero results.  The subquery just doesnt seem to take to "NOT IN".

 
04.16.2008 at 12:34PM PDT, ID: 21371196

Rank: Genius

NULL value(s) can cause havoc in NOT IN (and sometimes IN).  Please try this:


insert into staffsupervisors (supervisor)
select distinct supervisor
  from staffdetails
 where supervisor not in (select supervisor from staffsupervisors where supervisor is not null)
Accepted Solution
 
04.16.2008 at 01:02PM PDT, ID: 21371430
Bingo!  Wow - NULL values is really that evil.  I knew that concatenating with NULLS causes issues - well it just outputs NULL, but didnt think this would cause an issue.  Then again, I didnt think the subquery is that much different to a join (in terms of building its table for the comparison).

 
 
04.16.2008 at 01:11PM PDT, ID: 21371514
>> NULL values is really that evil. <<

Yes, they are for NOT IN / IN.

But it's logical in its own way.  

A comparison of anything to NULL always yields "unknown" (essentially).  Note that NOT unknown is also unknown.

On the other hand, a WHERE condition must be *TRUE* for a row to be included.

So if the comparison:

where supervisor not in (...)

returns "unknown", then the row will *not* be selected, because the condition is not *specifically true*.
 
 
04.17.2008 at 07:32PM PDT, ID: 21382906
"A comparison of anything to NULL always yields "unknown" (essentially)."

But not always.

x IN (...) can return TRUE even if the (...) expression list contains a NULL
x IS NULL returns TRUE if x is NULL

There are other exceptions too.

The designers of SQL tried hard to come up with some natural and consistent rules for handling three value logic - but they failed. SQL's handling of NULLs is neither intuitive nor particularly logical. So to achieve useful results with NULLs you need to learn about the quirks and live with them, which is no small task. The alternative is to avoid NULLs altogether, something which SQL unfortunately makes rather difficult because they tend to crop up in results even if you make all your columns non-nullable.


 
 
04.18.2008 at 06:49AM PDT, ID: 21385685
>> x IN (...) can return TRUE even if the (...) expression list contains a NULL <<

It *might*, or it might not, depending on where the searched for value and the NULL are in the list.

That's way too "flighty" to risk in actual use.  When using IN and NOT IN, one should **always** insure that a NULL(s) do not appear in the value list.
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628