The table I’m using has 3 important columns on which to exclude entries in a table. I’ll call them:
SampleTable:
Thing_Type (varchar)
Thing_ID (int)
Parent_ID (int)
I want to find all ‘leaf’ entries of a specific type. However, it is possible that an entry of that type has a child that is not of that type, and thus I don’t want to exclude it without first filtering the table to only that type. Then I want to include all entries which have an ID that is not present anywhere in the ParentID column.
There’s no EXISTS in the tool I’m using (not that I’m sure it would help).
Ignoring the fact that the tool I’m using doesn’t like the following, and that it might not be syntactically correct, here’s what I feel it should be like.
SELECT * FROM (
SELECT *
FROM SampleTable
WHERE SampleTable.Thing_Type = 'DesiredType'
)
WHERE Thing_ID NOT IN Parent_ID
I’m pretty sure this is wrong but I’m not sure how to make it right.
First,
NOT INhas to go against a set, not a single value, soNOT IN parent_iddoesn’t actually make sense. This is one way to approach this problem: