I have a legacy table that contains 'master' records and associated detail records. Detail records are identified by an addition to the master key of "-nnn". I need to find master records that have no detail records, tried several variations, and then finally broke it down to simplest elements.
This works:
select (select count(*) from dbo.client as cl2
where cl2.acct_no like (cl1.acct_no + '-%')) as countx, acct_no
from dbo.client as cl1
and displays the expected zero or non-zero results, depending on how many detail records there are.
However, when I try to use the count results to select only the records with zero detail records, like so:
select (select count(*) from dbo.client as cl2
where cl2.acct_no like (cl1.acct_no + '-%')) as countx, acct_no
from dbo.client as cl1
where countx = 0
I get an error: "Invalid column name 'countx'"
What am I missing here?