27,579
社区成员
发帖
与我相关
我的任务
分享
select * from employee_list t where
status='Y' and 类型 in (
select 类型 from employee_list
where t.编号=编号
group by 类型 ,编号 ,姓名
having count(1)=1 )
/*status 类型 编号 姓名
------ ---------- ---------- ----------
Y 拓展部分 0001 陈一
Y 拓展部分 0002 程二
(所影响的行数为 2 行)
*/
if object_id('[employee_list]') is not null drop table [employee_list]
go
create table [employee_list]([status] varchar(1),[类型] varchar(8),[编号] varchar(4),[姓名] varchar(4))
insert [employee_list]
select 'N','基础部分','0001','陈一' union all
select 'Y','基础部分','0001','陈一' union all
select 'Y','拓展部分','0001','陈一' union all
select 'N','专业部分','0001','陈一' union all
select 'N','基础部分','0002','程二' union all
select 'Y','基础部分','0002','程二' union all
select 'Y','拓展部分','0002','程二' union all
select 'N','专业部分','0002','程二'
-->查询
select *
from [employee_list] t
where not exists(select 1 from [employee_list] where 类型=t.类型 and 编号=t.编号 and status='N')
/**
status 类型 编号 姓名
------ -------- ---- ----
Y 拓展部分 0001 陈一
Y 拓展部分 0002 程二
(2 行受影响)
**/
select * from employee_list
group by 类型 ,编号 ,姓名,status
having status='Y' and count(1)=1
select *
from tb t
were not exists(select 1 from tb where 类型=t.类型 and 编号=t.编号 and status='N')