with A as (select to_date('2018-5-30 6:00:00','yyyy-mm-dd hh24:mi:ss') entrytime from dual union all
select to_date('2018-5-30 6:05:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 6:20:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 6:21:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 6:25:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 7:00:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 7:10:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 7:25:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 7:45:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 8:01:00','yyyy-mm-dd hh24:mi:ss') from dual),
B as (select to_date('2018-5-30 6:20:00','yyyy-mm-dd hh24:mi:ss') depart_time from dual union all
select to_date('2018-5-30 6:40:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 7:40:00','yyyy-mm-dd hh24:mi:ss') from dual union all
select to_date('2018-5-30 8:20:00','yyyy-mm-dd hh24:mi:ss') from dual)
select nexttime,count(1)
from A,
(select lag(depart_time,1) over(order by depart_time) firsttime,depart_time nexttime
from B
)
where (a.entrytime between firsttime and nexttime)
or (firsttime is null and entrytime<nexttime)
group by nexttime
order by nexttime
select t1.depart_time, count(1) from (
select*from (
select to_number(to_char(b.depart_time, 'sssss')) end_time,
to_number(to_char(nvl(lag(b.depart_time) over(order by b.depart_time), trunc(sysdate)),
'sssss')) start_time,
b.depart_time
from b b
) b1,
(select a.entry_time, to_number(to_char(b.depart_time, 'sssss')) s_time from a) a1
where a1.s_time(+) > b1.start_time
and a1.s_time(+) < b1.end_time
) t1
group by t1.depart_time
;
盲写的,不保证能执行...