gbn | mem_no | service_no | service_date | service_area |
01 | 1111 | 1 | 20130101 | aaa |
01 | 2222 | 2 | 20121201 | bbb |
02 | 2222 | 1 | 20130111 | bbb |
03 | 3333 | 3 | 20130304 | aaa |
04 | 1111 | 1 | 20130121 | aaa |
01 | 3333 | 3 | 20130101 | aaa |
03 | 4444 | 4 | 20130201 | ccc |
04 | 2222 | 2 | 20130121 | bbb |
01 | 1111 | 1 | 20130101 |
01 | 2222 | 2 | 20121201 |
04 | 1111 | 1 | 20130121 |
04 | 2222 | 2 | 20130121 |
with t (gbn, mem_no, service_no, service_date, service_area ) as ( select '01', '1111', '1', '20130101', 'aaa' from dual union all select '01', '2222', '2', '20121201', 'bbb' from dual union all select '02', '2222', '1', '20130111', 'bbb' from dual union all select '03', '3333', '3', '20130304', 'aaa' from dual union all select '04', '1111', '1', '20130121', 'aaa' from dual union all select '01', '3333', '3', '20130101', 'aaa' from dual union all select '03', '4444', '4', '20130201', 'ccc' from dual union all select '04', '2222', '2', '20130121', 'bbb' from dual ) select gbn , count(1) from t where gbn in ('01', '04') and service_date between '201211'||'01' and '201301'||'31' and exists ( select null from t a where a.mem_no = t.mem_no and a.gbn != t.gbn and substr(a.service_date,1,4) = substr(t.service_date,1,4) ) group by gbn order by gbn