t 테이블에 code 컬럼과 mail 컬럼이 있습니다.
여기서 찾고자 하는 자료는
code그룹 중 mail 컬럼이 모두 비어있는 code를 찾는건데요..
저는 아래와 같은 방법으로 했는데 이렇게 되면 소스 가독성도 떨어지고..
나중에 유지보수할때 손이 갈꺼같아서요.. 함수나 다른 방법으로 깔끔하게 만들 수 있는 방법이 없을까요?
with t as
(
select 'A001' code, '' mail from dual
union all
select 'A001' code, 'a' mail from dual
union all
select 'A001' code, '' mail from dual
union all
select 'A002' code, 'a' mail from dual
union all
select 'A003' code, 'b' mail from dual
union all
select 'A003' code, '' mail from dual
union all
select 'A004' code, '' mail from dual
union all
select 'A004' code, '' mail from dual
union all
select 'A004' code, '' mail from dual
union all
select 'A004' code, '' mail from dual
union all
select 'A005' code, 'a' mail from dual
union all
select 'A006' code, '' mail from dual
union all
select 'A007' code, '' mail from dual
union all
select 'A008' code, '' mail from dual
)
select *
from (select code,
sum(OK) OK,
sum(EMPTY ) EMPTY,
count(*) total
from (select code,
nvl2(mail, 1, 0) ok,
nvl2(mail, 0, 1) empty
from t a
)
group by code )
where EMPTY = TOTAL;