这个怎么样:
declare @tmp table (Id int, Nm varchar(50));
insert @tmp select 1, 'Harry Johns';
insert @tmp select 2, 'Adam Taylor';
insert @tmp select 3, 'John Smith';
insert @tmp select 4, 'John Smith';
insert @tmp select 5, 'Bill Manning';
insert @tmp select 6, 'John Smith';
select * from @tmp order by Id;
select Nm, count(1) from
(
select Id, Nm,
case when exists (
select 1 from @tmp t2
where t2.Nm=t1.Nm
and (t2.Id = t1.Id + 1 or t2.Id = t1.Id - 1))
then 1 else 0 end as Run
from @tmp t1
) truns group by Nm, Run
[编辑] 这可以缩短一点
select Nm, count(1) from (select Id, Nm, case when exists (
select 1 from @tmp t2 where t2.Nm=t1.Nm
and abs(t2.Id-t1.Id)=1) then 1 else 0 end as Run
from @tmp t1) t group by Nm, Run