--这样? declare @t table(a int,b int) insert into @t select 1,2 insert into @t select 1,3 insert into @t select 1,1 insert into @t select 2,4 insert into @t select 1,2 insert into @t select 3,2select count(1) from (select a from @t group by a having count(*)=1)t
select sum(1) from Ta TT where not exists (select 1 from Ta where a=TT.a)
--这样? declare @t table(a int,b int) insert into @t select 1,2 insert into @t select 1,3 insert into @t select 1,1 insert into @t select 2,4 insert into @t select 1,2 insert into @t select 3,2select count(1) from (select a from @t group by a having count(*)=1)t
--测试环境 create table mytab ( id int, a varchar(10) ) insert into mytab select 1,'a' insert into mytab select 2,'b' insert into mytab select 3,'b' insert into mytab select 4,'c' insert into mytab select 5,'d' insert into mytab select 6,'d'--查询所有不重复的a,结果为4 select count(1) from (select distinct a from mytab)a --查询没有重复行的a,结果为2 select count(1) from (select a from mytab group by a having count(1) < 2)a
select sum(1) from Ta TT where not exists (select 1 from Ta where a=TT.a) ---->更正select sum(1) from ( select a from @t group by a having count(*)=1 ) t
xeqtr1982(vesslan)的数据 select * from @t group by a,b having count(*)=1
declare @t table(id int,a varchar(10)) insert into @t select 1,'a' union all select 2,'a' union all select 3,'b' union all select 4,'b' union all select 5,'c'select count(*) from (select a from @t group by a)t
coolingpipe(冷箫轻笛)厉害.谢谢, 我要的就是这个一种,select count(1) from (select distinct a from mytab)a
declare @t table(a int,b int)
insert into @t select 1,2
insert into @t select 1,3
insert into @t select 1,1
insert into @t select 2,4
insert into @t select 1,2
insert into @t select 3,2select count(1) from (select a from @t group by a having count(*)=1)t
where not exists (select 1 from Ta where a=TT.a)
declare @t table(a int,b int)
insert into @t select 1,2
insert into @t select 1,3
insert into @t select 1,1
insert into @t select 2,4
insert into @t select 1,2
insert into @t select 3,2select count(1) from (select a from @t group by a having count(*)=1)t
create table mytab
(
id int,
a varchar(10)
)
insert into mytab select 1,'a'
insert into mytab select 2,'b'
insert into mytab select 3,'b'
insert into mytab select 4,'c'
insert into mytab select 5,'d'
insert into mytab select 6,'d'--查询所有不重复的a,结果为4
select count(1) from (select distinct a from mytab)a
--查询没有重复行的a,结果为2
select count(1) from (select a from mytab group by a having count(1) < 2)a
where not exists (select 1 from Ta where a=TT.a)
---->更正select sum(1) from
(
select a from @t
group by a
having count(*)=1
) t
select * from @t group by a,b having count(*)=1
1,'a'
2,'a'
3,'b'
4,'b'
5,'c'计算结果:
行数:3一条sql语句可以做到吗?
insert into @t select 1,'a'
union all select 2,'a'
union all select 3,'b'
union all select 4,'b'
union all select 5,'c'select count(*) from (select a from @t group by a)t
我要的就是这个一种,select count(1) from (select distinct a from mytab)a
from t
group by a不可以吗