sorry for the confusing title.
I have two tables, individu and orgcont. individu looks something like:
individu
-------------
ind_id
org_id
email
orgcont looks something like:
orgcont
-------------
ind_id
function_code
I have many users that can belong to the same org_id and they each of their own ind_id. The ‘leaders’ of the org_id have a function code of ‘PRIM’, everyone else’s are irrelevant. I am trying to return a list of org_id and email of all of the users who have a ‘PRIM’ function code who have users that have NULL email addresses that are not ‘PRIM’.
I have something like this, but the results are not correct. Any help would be appreciated:
select
i.email,
i.org_id
from
individu i,
orgcont o,
individu i2,
orgcont o2
where
i.ind_id = o.ind_id
and i.org_id = i2.org_id
and i2.ind_id = o2.ind_id
and o.function_code = 'PRIM'
and o2.function_code != 'PRIM'
and i2.email is NULL
Try it with
exists:This will only return you one row for each leader with folks in their org with
nullemail addresses, as opposed to your original query, which will return duplicate rows.Also note the join syntax–you want to do an
inner joinhere, not across join, so do so explicitly.Additionally, you could have just done a
select distinct, but that’s a little more expensive than theexists.