我有这张桌子:
id id_emp ch_in type date ======================================== 1 1 09:12 参加2019 -08-04 2 1 NULL 缺席 2019-08-04 3 2 09:20 参加 2019-08-04 4 2 NULL 缺席 2019-08-04 5 3 NULL 缺席 2019-08-04 6 1 NULL 缺席 2019-08 05
我想选择所有缺席的员工 ID ( id_emp
)。但是,如果员工ch_in
在同一日期已经有一个不为空的更早的,他就不会缺席。
所以在样本数据中,结果将是:
结果必须是:
id id_emp ch_in type date ====================================== 5 3 NULL 不存在 2019-08 -04 6 1 NULL 不存在 2019-08-05
我厌倦了这样做:
select distinct id_emp,ch_in,type,date
FROM ch_inout
where absent= 'absent'
但我不能让它工作。我怎样才能得到正确的结果?
不存在:
select c.* from ch_inout c
where c.type = 'absent'
and not exists (
select 1 from ch_inout
where id_emp = c.id_emp and date = c.date and ch_in is not null
)
请参阅演示。
结果:
> id | id_emp | ch_in | type | date
> -: | -----: | :---- | :----- | :---------
> 5 | 3 | null | absent | 04/08/2019
> 6 | 1 | null | absent | 05/08/2019
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句