Solved

SQL Query to get orphaned nodes

Posted on 2014-11-11
4
224 Views
Last Modified: 2014-11-11
Hi all,
I have an interesting query that I am trying to create...

I have a table that contains entities, each of these *may* have a parent (only single level)

What I would like to do is query the table to find entries that say they have a parent, but the parent entry does not exist.

tblData
ID, DESCRIPTION, PARENT
'A','Main Parent','-'
'B','First Child','A'
'C','Second Child','A'
'D','Another Parent','-'
'E','An Orphan','Z'

In this example, I would like to execute a query to get the record relating to 'E'

I've tried a few ways, but can't get it to work!!!

Any ideas would be very much appreciated..

Thanks a lot,

James
0
Comment
Question by:jatkin
4 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 total points
ID: 40434781
select a.*
from tblData a
left join tblData b on a.Parent = b.ID
where a.Parent != '-' and b.ID is null

Open in new window

0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40434783
Try this:

Select T.ID
from tblData T
LEFT JOIN tblData U
on T.[Parent] = U.ID
where U.ID is null

Open in new window

0
 
LVL 33

Expert Comment

by:ste5an
ID: 40434792
You should use a foreign key constraint to avoid this situation.

SELECT  C.*
FROM    #tblData C
        LEFT JOIN #tblData P ON C.PARENT = P.ID
WHERE   P.ID IS NULL;

Open in new window

0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40434793
Perfect!!! Thanks :-)
Hope it is okay to split points equally as there was only 1 minute between posts!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 32
Linked Server Issue with SQL2012 3 26
Create snapshot on MSSQL 2012 3 19
SQL invalid column name 5 13
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

777 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