我有一张表,其中包含非出勤的开始和结束日期,我希望获得按ID和按月分组的缺勤天数。我通过计算日期+1之间的差异来做到这一点。问题是有时日期与彼此重叠。这是一个例子:
ID ABS_START ABS_END NBR_ABS
5 04/02/2016 04/02/2016 1
5 05/02/2016 05/02/2016 1
5 06/02/2016 07/02/2016 2
5 07/02/2016 20/02/2016 14
5 08/02/2016 14/02/2016 7
NBR_ABS = number of absences relative to dates period
预期结果:
ID ABS_MONTH NBR_ABS_MONTH
5 2016/02 17
6 2016/02 0
7 2016/02 5
8 2016/02 13
9 2016/02 2
NBRE_ABS_MONTH = number of absences by id for whole month
有没有办法在oracle环境中处理这样的问题? 谢谢你的回答!
答案 0 :(得分:0)
一种简单的方法是获取范围内的日期列表并进行检查。例如:
with dates as (
select (date '2016-02-01') + rownum - 1
from all_objects
where rownum <= 29
)
select i.id, count(*) as absence_201602
from dates cross join
(select distinct id from t) i
where exists (select 1
from t
where t.id = i.id and
d.date between t.abs_start and t.abs_end
)
group by id;