首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于 group

2012-05-08 
请教高手关于 group我的数据库中有A字段B字段a1a1a2b1b1b1b1c1c1d1我现在想用group 的同时 只显示 count

请教高手关于 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(无列名)
b1
c1
d1
*/

可是这样??

热点排行