我的数据库中有
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
谢谢大家指点一下!!
我的数据库中有
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',1select A,COUNT(distinct B) from tbl group by A having count(distinct B)=1/*
A (无列名)
b 1
c 1
d 1
*/可是这样??
(
select distinct a , b from tb
) t
group by a , b having count(1) = 1
insert tb
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',1select a , 1 b from
(
select distinct a , b from tb
) t
group by a having count(1) = 1drop table tb/*
a b
---------- -----------
b 1
c 1
d 1(所影响的行数为 3 行)
*/
select A,COUNT(distinct B)
from tbl
group by A
having count(distinct B) = 1 and max(B) = 1
我加了图了。请大家帮我看下
http://topic.csdn.net/u/20120223/11/3f94e0b8-5ab9-4e1f-9989-00318b7d8c53.html?43476