select distinct a.*
from main a join
( select a
from detail b
where not exists ( select 1
from detail
where flag<>y and a=b.a
)
) c
on a.a=b.a
from main a join
( select a
from detail b
where not exists ( select 1
from detail
where flag<>y and a=b.a
)
) c
on a.a=b.a
from main a join
( select a
from detail b
where not exists ( select 1
from detail
where flag<>'y' and a=b.a
)
) c
on a.a=b.a
from main a join
( select a
from detail b
where not exists ( select 1
from detail
where flag<>'y' and a=b.a
)
) c
on a.a=c.a