34,838
社区成员




declare @tb table (id int,col varchar(1))
insert into @tb select 1,'a'
insert into @tb select 2,'b'
insert into @tb select 3,'c'
insert into @tb select 4,'d'
insert into @tb select 5,'a'
insert into @tb select 6,'b'
select count(1) from @tb a where not exists (select 1 from @tb b where a.id<>b.id and a.col = b.col)
declare @tb table (id int,col varchar(1))
insert into @tb select 1,'a'
insert into @tb select 2,'b'
insert into @tb select 3,'c'
insert into @tb select 4,'d'
insert into @tb select 5,'a'
insert into @tb select 6,'b'
select sum(con) as '次数' from (
select con=(select count(1) from @tb where col=t.col and id>t.id) from @tb t)t