我有一张表,该表将为每次发生的每次更新添加条目,这是我的数据的状态
ID EmpId ischange
1 100 0
2 100 1
3 101 0
4 101 0
5 101 0
我需要的是一个查询,如果至少有1名员工有1个员工,那么它将返回ischange值为0,那么我不希望显示该记录。
我只需要ID为101的记录
答案 0 :(得分:3)
选择总行数等于isChange
为0
的行。
查询
select [EmpId] from [tableName]
group by [EmpId]
having sum(case [ischange] when 0 then 1 else 0 end) = count([EmpId]);
或者,如果您想要整行,则
查询
select * from [tableName] as [t1]
where not exists(
select 1 from [tableName] as [t2]
where [t1].[EmpId] = [t2].[EmpId]
and [ischange] = 1
);
答案 1 :(得分:2)
简单易懂的方式:
select * from mytable
where EmpId not in (
select EmpId from mytable
where ischange = 1)
更高效的方式(假设EmpId
上有一个索引):
select a.*
from mytable a
left join mytable b on b.EmpId = a.EmpId
and b.ischange = 1
where b.EmpId is null
答案 2 :(得分:2)
或更高效的方式:
select * from myTable t1
where not exists
(select * from myTable t2
where t1.empId=t2.empId and t2.ischange=1);
答案 3 :(得分:0)
residual = z - fittedmodel(x,y);
percentageResidual = residual./z*100;