22,302
社区成员




create table liq
(id int, name varchar(5), value varchar(5))
insert into liq
select 1, 'a', '0' union all
select 2, 'b', '0' union all
select 3, 'b', '1' union all
select 4, 'c', '0' union all
select 5, 'c', '0' union all
select 6, 'a', '1' union all
select 7, 'a', '0'
select distinct name
from liq a
where not exists
(select 1
from liq b where b.name=a.name and b.value='1')
/*
name
-----
c
(1 row(s) affected)
*/
create table #tb
(id int,name varchar(5),value int)
insert into #tb
select 1 ,'a', 0 union all
select 2 ,'b', 0 union all
select 3 ,'b', 1 union all
select 4 ,'c', 0 union all
select 5 ,'c', 0 union all
select 6 ,'a', 1 union all
select 7 ,'a', 0
select name from #tb
group by name having MAX(value)=0
select
name
from
(select * from #tb where value<>1) t
group by name
having COUNT(name)=1
lz给出的结果存在问题吧
怎么会显示C呢,
4 c 0
5 c 0
这个c已经重复了
create table #tb
(id int,name varchar(5),value int)
insert into #tb
select 1 ,'a', 0 union all
select 2 ,'b', 0 union all
select 3 ,'b', 1 union all
select 4 ,'c', 0 union all
select 5 ,'c', 0 union all
select 6 ,'a', 1 union all
select 7 ,'a', 0
select name from #tb
group by name having MAX(value)=0
name
-----
c
(1 行受影响)
SELECT [name],SUM(ISNULL(value,0)) AS Value
FROM tablename
GROUP BY [name] HAVING SUM(ISNULL(value,0))=0