获取间隔时间范围内日期数据的开始时间结束时间

create table A_2
(
  pscode      VARCHAR2(12),
  outputcode  VARCHAR2(3),
  monitortime DATE,
  processname VARCHAR2(50)
);

insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:27:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:27:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:37:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:38:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:40:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:45:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:47:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:48:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:37:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:38:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:40:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:45:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:47:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:48:00', 'dd-mm-yyyy hh24:mi:ss'), 'SunloginClient.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:37:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:38:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:40:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:45:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:47:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 14:48:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:37:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:38:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:40:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:42:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:45:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:47:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
insert into A_2 (pscode, outputcode, monitortime, processname)
values ('410100001876', '1', to_date('27-08-2020 16:48:00', 'dd-mm-yyyy hh24:mi:ss'), 'WebChat.exe');
commit;

数据效果:

 

统计效果:

统计SQL


-- 第1种方案,有所欠缺
select m.pscode,m.outputcode,m.processname,m.startdate,max(monitortime) enddate
  from (select pscode,outputcode,monitortime,processname,
               (select min(monitortime) from a_2 e where ceil((s.monitortime - e.monitortime) * 24 * 60) <= 30) startdate
          from a_2 s
       ) m 
 group by m.pscode, m.outputcode, m.processname, m.startdate


-- 第2种方案,适应各种情况
select pscode,outputcode,processname,monitortime startdate,
       (select max(monitortime) from a_2 m 
         where m.pscode=n.pscode 
           and m.outputcode=n.outputcode 
           and m.processname=n.processname 
           and ((n.enddate is null and m.monitortime >= n.monitortime) or ( m.monitortime>=n.monitortime and m.monitortime<n.enddate) )
         
       ) enddate
  from (select pscode,outputcode,processname,monitortime,lead(s.monitortime,1,NULL) over(partition by pscode,outputcode,processname order by monitortime) enddate
          from (select pscode,outputcode,monitortime,processname,  
                       ceil((lag(monitortime,1,monitortime) over(partition by pscode,outputcode,processname order by monitortime) - monitortime) * 24 * 60) intervals,
                       row_number() over(partition by pscode,outputcode,processname order by monitortime) orderby
                  from a_2
               ) s
         where orderby=1 or intervals<-8 --时间间隔,8分钟内是否有数据
        ) n

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值