Fastest way to find parents with only one child?
450517Sep 20 2005 — edited Sep 20 2005I have two very large tables (both >6 million rows) in an oracle 8i DB. They have a parent child relationship and I would like to construct a query to give me the parents that have only one child......syntactically, what's the best way to construct this query?
I was going to try:
select join_id
FROM parent
where join_id in (select join_id, count(join_id)
FROM child
group by join_id
having count(*)=1)
but then I realized that the subselect has two columns to return and the primary query is only expecting one column, so this will not work.
I suspect there's a quick and dirty way to find out what parents have only one child....
I thought of using rowid's but am not sure this is the best way and in the example below I tried, I'm having problems b/c of functions not being allowed in the where clause.....
select join_id
from child d
where rowid in (select min(rowid)
FROM child s
WHERE min(d.rowid)=max(s.rowid)
AND s.join_id=d.join_id))
Any thoughts?