with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual
)
select * from t;
select username, opttime, floor(cnt / 3) times
from (select username,
opttime,
opttime_dt,
count(1) over(partition by username, opttime_dt - flag / 1440) cnt
from (select t.*,
to_date(t.opttime, 'yyyy-mm-dd hh24:mi') opttime_dt,
row_number() over(partition by t.username order by opttime) flag
from t))
where cnt >= 3;
手头没有环境,没法验证。试试看吧
输出的是存在违规的相关记录,times为违规次数
#4
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual),
t1 as (select t.*,count(1) over (partition by username order by opttime range interval '1' minute preceding) cc from t),
t2 as (select t1.*,sum(case cc when 2 then 1 else 0 end) over (partition by username order by opttime)+1 dd from t1),
t3 as (select t2.*,case when trunc(dd/3)*3=dd then 1 else 0 end ee from t2)
select username,sum(ee) from t3 group by username;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;
tot>0为你要的结果
#8
楼主要求计算出“每次“的违规记录,在我理解来每次就是指连续的
#9
应该就是连续的意思吧,如果出现断点的话,那就要从这个间断时间点开始重新计时3分钟,否则就说不过去了
#10
select username, opttime, floor(cnt / 3) times
from (select username,
opttime,
opttime_dt,
count(1) over(partition by username, opttime_dt - flag / 1440) cnt
from (select t.*,
to_date(t.opttime, 'yyyy-mm-dd hh24:mi') opttime_dt,
row_number() over(partition by t.username order by opttime) flag
from t))
where cnt >= 3;
手头没有环境,没法验证。试试看吧
输出的是存在违规的相关记录,times为违规次数
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'A' username, '2016-09-08 09:14' opttime from dual union all
select 'A' username, '2016-09-08 09:14' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'A' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 09:20' opttime from dual
)
select username,sum(a)
from (select username, max(floor(cnt / 3)) a
from (select username,
opttime,
opttime_dt,
count(1) over(partition by username, opttime_dt - flag / 1440) cnt,FLAG
from (select username,opttime,
to_date(t.opttime, 'yyyy-mm-dd hh24:mi') opttime_dt,
dense_rank() over(partition by t.username order by opttime) flag
from t group by username,opttime))
where cnt >= 3 group by username,opttime_dt - flag / 1440)
group by username;
#11
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual),
t1 as (select t.*,count(1) over (partition by username order by opttime range interval '1' minute preceding) cc from t),
t2 as (select t1.*,sum(case cc when 2 then 1 else 0 end) over (partition by username order by opttime)+1 dd from t1),
t3 as (select t2.*,case when trunc(dd/3)*3=dd then 1 else 0 end ee from t2)
select username,sum(ee) from t3 group by username;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;
select username, opttime, floor(cnt / 3) times
from (select username,
opttime,
opttime_dt,
count(1) over(partition by username, opttime_dt - flag / 1440) cnt
from (select t.*,
to_date(t.opttime, 'yyyy-mm-dd hh24:mi') opttime_dt,
row_number() over(partition by t.username order by opttime) flag
from t))
where cnt >= 3;
手头没有环境,没法验证。试试看吧
输出的是存在违规的相关记录,times为违规次数
#4
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual),
t1 as (select t.*,count(1) over (partition by username order by opttime range interval '1' minute preceding) cc from t),
t2 as (select t1.*,sum(case cc when 2 then 1 else 0 end) over (partition by username order by opttime)+1 dd from t1),
t3 as (select t2.*,case when trunc(dd/3)*3=dd then 1 else 0 end ee from t2)
select username,sum(ee) from t3 group by username;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;
select username, opttime, floor(cnt / 3) times
from (select username,
opttime,
opttime_dt,
count(1) over(partition by username, opttime_dt - flag / 1440) cnt
from (select t.*,
to_date(t.opttime, 'yyyy-mm-dd hh24:mi') opttime_dt,
row_number() over(partition by t.username order by opttime) flag
from t))
where cnt >= 3;
手头没有环境,没法验证。试试看吧
输出的是存在违规的相关记录,times为违规次数
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'A' username, '2016-09-08 09:14' opttime from dual union all
select 'A' username, '2016-09-08 09:14' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'A' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 09:20' opttime from dual
)
select username,sum(a)
from (select username, max(floor(cnt / 3)) a
from (select username,
opttime,
opttime_dt,
count(1) over(partition by username, opttime_dt - flag / 1440) cnt,FLAG
from (select username,opttime,
to_date(t.opttime, 'yyyy-mm-dd hh24:mi') opttime_dt,
dense_rank() over(partition by t.username order by opttime) flag
from t group by username,opttime))
where cnt >= 3 group by username,opttime_dt - flag / 1440)
group by username;
#11
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual),
t1 as (select t.*,count(1) over (partition by username order by opttime range interval '1' minute preceding) cc from t),
t2 as (select t1.*,sum(case cc when 2 then 1 else 0 end) over (partition by username order by opttime)+1 dd from t1),
t3 as (select t2.*,case when trunc(dd/3)*3=dd then 1 else 0 end ee from t2)
select username,sum(ee) from t3 group by username;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;
with t as(
select 'A' username, '2016-09-08 09:10' opttime from dual union all
select 'B' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:11' opttime from dual union all
select 'A' username, '2016-09-08 09:12' opttime from dual union all
select 'C' username, '2016-09-08 09:13' opttime from dual union all
select 'A' username, '2016-09-08 09:15' opttime from dual union all
select 'A' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'B' username, '2016-09-08 09:17' opttime from dual union all
select 'A' username, '2016-09-08 09:18' opttime from dual union all
select 'B' username, '2016-09-08 09:19' opttime from dual union all
select 'A' username, '2016-09-08 10:10' opttime from dual union all
select 'A' username, '2016-09-08 10:11' opttime from dual union all
select 'A' username, '2016-09-08 10:12' opttime from dual
)
select username,opttime,back,front,sum(case when to_date(opttime,'yyyy-mm-dd hh24:mi')-to_date(back,'yyyy-mm-dd hh24:mi')=1/(24*60)
and to_date(front,'yyyy-mm-dd hh24:mi')-to_date(opttime,'yyyy-mm-dd hh24:mi')=1/(24*60) then 1 else 0 end) tot
from (
select username,opttime,
lead(opttime) over(partition by username order by opttime desc) back,
lead(opttime) over(partition by username order by opttime) front
from t)
group by username,username,opttime,back,front
;