Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Not Exists
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01219446
Message ID:
01219472
Views:
8
>>>What I need:
>>> Return record(s) that are not found in the Parent Tables
>>>
>>>Combined Table has Fields ID,AssemblyId,PartsId
>>>
>>>Parent Table One has ID,AssemblyId
>>>
>>>Parent Table Two has ID,PartsId
>>>
>>>What I tried that returns all records when I want only records that are not in parent tables.
>>>
>>>Select *
>>>FROM Combined
>>>Where exists
>>>(
>>>  SELECT a.*
>>>  FROM Combined a
>>>  WHERE somefield<>'2'
>>>  and NOT EXISTS
>>>   (
>>>     SELECT id
>>>     FROM ParentTwo b
>>>     WHERE a.id = b.Id
>>>     and a.PartsId = b.PartsId
>>>     and not exists
>>>     (
>>>       SELECT id
>>>       FROM ParentOne c
>>>       where a.Id = c.Id and a.AssemblyId = c.AssemblyId
>>>     )
>>>   )
>>>)
>>>
>>>Any pointers on where I'm screwing up would be welcomed.
>>>
>>>TIA
>>>
>>>Jeff
>>
>>Try
>>
>>
>>SELECT c.* FROM Combined c LEFT Join Parent1 p1 on c.ID = P1.ID
>>LEFT JOIN Parent2 p2 on c.ID = p2.ID where p1.ID IS NULL or p2.ID IS NULL
>
>That will return the record even if it is in one of the parent records, just change OR to AND :-)
>
>But I prefer this:
>
>SELECT Combined.*
>       FROM Combined
>LEFT JOIN (SELECT Id FROM Parent1
>           UNION
>           SELECT Id FROM Parent2) Tbl1
>ON Combined.Id = Tbl1.Id
>WHERE Tbl1.Id IS NULL
>
>
>or
>
>
>SELECT Combined.*
>       FROM Combined
>WHERE Combined.Id NOT IN
>      (SELECT Id FROM Parent1
>       UNION
>       SELECT Id FROM Parent2)
>
AND means that we don't have this record in Parent1 and in Parent2. I'm not sure I got the exact idea of what was Jeff asking.
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform