你可以这样做:
SQL> with data as (select 'A' Merge1, 'Y' Merge2 from dual union all
2 select 'A', 'Y' from dual union all
3 select 'A', 'Y' from dual union all
4 select 'B', 'Y' from dual union all
5 select 'B', 'Y' from dual union all
6 select 'B', 'Y' from dual union all
7 select 'C', 'N' from dual union all
8 select 'C', 'N' from dual union all
9 select 'C', 'N' from dual union all
10 select 'D', 'N' from dual union all
11 select 'D', 'N' from dual union all
12 select 'E', null from dual union all
13 select 'E', null from dual union all
14 select 'F', null from dual union all
15 select 'F', null from dual union all
16 select null, null from dual)
17 select merge1, max(merge2), count(*)
18 from (select merge1, merge2,
19 case when merge2 = 'Y' or merge2 is null then merge2 else to_char(rownum) end grp
20 from data)
21 group by merge1, grp
22 order by merge1;
M M COUNT(*)
- - ----------
A Y 3
B Y 3
C N 1
C N 1
C N 1
D N 1
D N 1
E 2
F 2
1
测试小提琴:http ://sqlfiddle.com/#!4/b85cc/1