Groups | Blog | Home
all groups > sql server programming > august 2003 >

sql server programming : Help with Select not in (select...)


Leo Violette
8/26/2003 11:48:00 PM
I'm using MS Sql Server MSDE

Table1
shotID NextShotID CaromShotID
1 2 3
2 NULL NULL
3 NULL NULL

I want to select the row where NextShotID is null and shotID does not exist
in the CaromShotID column.

Since ShotID1 contains a non-null shotID2, it should be omitted.
Since ShotID3 is found in the CaromShotID column for ShotID1, it shoud be
omitted.
This should leave ShotID2.

I've read that using Not in here with columns that return NULL is a problem.
How would I rewrite?

My current attempt returns no rows.

I try:
select *
From Table1
Where CaromShotID not in
(
Select ShotID
From Table1
Where NextShotID IS NULL
)


Hope this makes sense.

microsoft.public.sqlserver.programming




oj
8/26/2003 11:57:42 PM
select *
From Table1 t1
Where not exists
(
Select *
From Table1 t2
Where NextShotID IS NULL
and t2.ShotID = t1.CaromShotID
)


--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net



[quoted text, click to view]

Leo Violette
8/27/2003 12:46:33 AM
Thank you very much!

It was almost perfect, I had to change it to:
select *
From Table1 t1
Where not exists
(
Select *
From Table1 t2
Where t2.NextShotID IS NULL
and t2.CaromShotID = t1.ShotID
)

[quoted text, click to view]

AddThis Social Bookmark Button