27,580
社区成员
发帖
与我相关
我的任务
分享
select * from tb t where not exists ( select * from tb where id=t.id and flag='N'
select * from tb a
where a.FLAG='Y'
and not exists
(select 1 from tb b where a.id=b.id and b.FLAG<>'Y)'
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[FLAG] varchar(1))
insert [tb]
select 1,'Y' union all
select 1,'Y' union all
select 1,'N' union all
select 2,'Y' union all
select 2,'N' union all
select 2,'Y' union all
select 3,'Y' union all
select 3,'Y' union all
select 3,'Y' union all
select 4,'N' union all
select 4,'N' union all
select 4,'N'
select * from tb t where not exists(select 1 from tb where id=t.id and flag!='Y')
/**
ID FLAG
----------- ----
3 Y
3 Y
3 Y
(3 行受影响)
**/