日期:2014-05-18  浏览次数:20392 次

请教高手关于 group
我的数据库中有
A字段 B字段

a 1
a 1
a 2
b 1
b 1
b 1
b 1
c 1
c 1
d 1

我现在想用group 的同时 只显示 count =1
的要怎么做?
也就是上面的数据 group 后
只得到
b 1
c 1
d 1
谢谢大家指点一下!!

------解决方案--------------------
/*
我的数据库中有
A字段 B字段

a 1
a 1
a 2
b 1
b 1
b 1
b 1
c 1
c 1
d 1

我现在想用group 的同时 只显示 count =1
的要怎么做?
也就是上面的数据 group 后
只得到
b 1
c 1
d 1
谢谢大家指点一下!!
*/
go
if object_id('tbl')is not null
drop table tbl
go
create table tbl(
A varchar(1),
B int
)
go
insert tbl
select 'a',1 union all
select 'a',1 union all
select 'a',2 union all
select 'b',1 union all
select 'b',1 union all
select 'b',1 union all
select 'b',1 union all
select 'c',1 union all
select 'c',1 union all
select 'd',1



select A,COUNT(distinct B) from tbl group by A having count(distinct B)=1

/*
A (无列名)
b 1
c 1
d 1
*/

可是这样??