17,086
社区成员
发帖
与我相关
我的任务
分享
with tab_0 as(
select 1 rowid_, 1 id, to_Date('20200103 004254', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 2 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 3 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 4 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 4 rowid_, 1 id, to_Date('20200103 000002', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 002051', 'YYYYMMDD HH24MISS') to_ from dual union all
select 5 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 6 rowid_, 1 id, to_Date('20200103 004235', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 024830', 'YYYYMMDD HH24MISS') to_ from dual
)
SELECT ID, SUM(C)
FROM (SELECT ID, RN, (MAX(TO_) - MIN(FROM_)) * 24 C
FROM (SELECT ID, FROM_, TO_,
SUM(F) OVER(PARTITION BY ID ORDER BY FROM_) AS RN
FROM (SELECT T.*,
CASE
WHEN MAX(TO_)
OVER(PARTITION BY ID ORDER BY FROM_
ROWS BETWEEN UNBOUNDED PRECEDING AND 1
PRECEDING) >= FROM_ THEN
0
ELSE
1
END AS F
FROM TAB_0 T))
GROUP BY ID, RN)
GROUP BY ID
with tab_0 as(
select 1 rowid_, 1 id, to_Date('20200103 004254', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 2 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 3 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 4 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 4 rowid_, 1 id, to_Date('20200103 000002', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 002051', 'YYYYMMDD HH24MISS') to_ from dual union all
select 5 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235950', 'YYYYMMDD HH24MISS') to_ from dual union all
select 6 rowid_, 1 id, to_Date('20200103 004235', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 024830', 'YYYYMMDD HH24MISS') to_ from dual
)
SELECT ID, SUM(C)
FROM (SELECT ID, (MAX(TO_) - MIN(FROM_)) * 24 C
FROM (SELECT ID, FROM_, TO_,
SUM(F) OVER(PARTITION BY ID ORDER BY FROM_) AS RN
FROM (SELECT T.*,
CASE
WHEN LAG(TO_)
OVER(PARTITION BY ID ORDER BY FROM_) >= FROM_ THEN
0
ELSE
1
END AS F
FROM TAB_0 T))
GROUP BY ID, RN)
GROUP BY ID
with tab_0 as(
select 1 rowid_, 1 id, to_Date('20200103 004254', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235959', 'YYYYMMDD HH24MISS') to_ from dual union all
select 2 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235959', 'YYYYMMDD HH24MISS') to_ from dual union all
select 3 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235959', 'YYYYMMDD HH24MISS') to_ from dual union all
select 4 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235959', 'YYYYMMDD HH24MISS') to_ from dual union all
select 5 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235959', 'YYYYMMDD HH24MISS') to_ from dual union all
select 6 rowid_, 1 id, to_Date('20200103 004235', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 024830', 'YYYYMMDD HH24MISS') to_ from dual
)
SELECT ID, SUM(C)
FROM (SELECT ID, (MAX(TO_) - MIN(FROM_)) * 24 C
FROM (SELECT ID, FROM_, TO_,
SUM(F) OVER(PARTITION BY ID ORDER BY FROM_) AS RN
FROM (SELECT T.*,
CASE
WHEN LAG(TO_)
OVER(PARTITION BY ID ORDER BY FROM_) >= FROM_ THEN
0
ELSE
1
END AS F
FROM TAB_0 T))
GROUP BY ID, RN)
GROUP BY ID
with tab_0 as(
select 1 rowid_, 1 id, to_Date('20200103 004254', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235959', 'YYYYMMDD HH24MISS') to_ from dual union all
select 2 rowid_, 1 id, to_Date('20200103 000001', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 235959', 'YYYYMMDD HH24MISS') to_ from dual union all
select 3 rowid_, 1 id, to_Date('20200103 004235', 'YYYYMMDD HH24MISS') from_, to_Date('20200103 024830', 'YYYYMMDD HH24MISS') to_ from dual --union all
--select 2 rowid_, 1 id, to_Date('20200102103339', 'YYYYMMDDHH24MISS') from_, to_Date('20200102103340', 'YYYYMMDDHH24MISS') to_ from dual
)
--select * from tab_0;
, tab0 as (
select * from tab_0 t1 where not exists (
select * from tab_0 v1
where t1.from_ > v1.from_ and t1.from_ < v1.to_
and t1.to_ > v1.from_ and t1.to_ < v1.to_)
)
--select * from tab0;
, tab1 as (
select t1.rowid_,
t1.id,
t1.from_,
nvl(t1.to_, sysdate) to_
from tab0 t1
)
--select * from tab1;
, tab2 as (
select t1.*,
--lag(t1.to_) over(partition by t1.id order by t1.from_) lag_to,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.to_)), t1.from_) true_from,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.to_), t1.to_), t1.to_) true_to
from tab1 t1
where 1 = 1
order by nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.to_)), t1.from_)
)
--select * from tab2;
, tab3 as(
select t1.*,
decode(level, 1, t1.true_from, trunc(t1.true_from) + level - 1) true_level_from,
decode(level, max(level) over(partition by t1.rowid_), true_to, trunc(t1.true_to) - (max(level) over(partition by t1.rowid_) - level - 1)) true_level_to,
level
from tab2 t1
connect by prior t1.rowid_ = t1.rowid_
and prior dbms_random.value is not null
and level <= trunc(t1.true_to) - trunc(t1.true_from) + 1
)
--select t1.*, t1.true_level_to - t1.true_level_from from tab3 t1;
select t1.id,
trunc(t1.true_level_from) day_,
sum(t1.true_level_to - t1.true_level_from) * 24
from tab3 t1
group by t1.id,
trunc(t1.true_level_from)
;
with tab0 as(
select 1 rowid_, 1 id, to_Date('20200103004254', 'YYYYMMDDHH24MISS') from_, to_Date('20200103235959', 'YYYYMMDDHH24MISS') to_ from dual union all
select 2 rowid_, 1 id, to_Date('20200103000001', 'YYYYMMDDHH24MISS') from_, to_Date('20200103235959', 'YYYYMMDDHH24MISS') to_ from dual union all
select 3 rowid_, 1 id, to_Date('20200103004235', 'YYYYMMDDHH24MISS') from_, to_Date('20200103024830', 'YYYYMMDDHH24MISS') to_ from dual --union all
--select 2 rowid_, 1 id, to_Date('20200102103339', 'YYYYMMDDHH24MISS') from_, to_Date('20200102103340', 'YYYYMMDDHH24MISS') to_ from dual
)
, tab1 as (
select t1.rowid_,
t1.id,
t1.from_,
nvl(t1.to_, sysdate) to_
from tab0 t1
)
--select * from tab1;
, tab2 as (
select t1.*,
--lag(t1.to_) over(partition by t1.id order by t1.from_) lag_to,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.to_)), t1.from_) true_from,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.to_), t1.to_), t1.to_) true_to
from tab1 t1
where 1 = 1
order by nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.to_)), t1.from_)
)
--select * from tab2;
, tab3 as(
select t1.*,
decode(level, 1, t1.true_from, trunc(t1.true_from) + level - 1) true_level_from,
decode(level, max(level) over(partition by t1.rowid_), true_to, trunc(t1.true_to) - (max(level) over(partition by t1.rowid_) - level - 1)) true_level_to,
level
from tab2 t1
connect by prior t1.rowid_ = t1.rowid_
and prior dbms_random.value is not null
and level <= trunc(t1.true_to) - trunc(t1.true_from) + 1
)
--select t1.*, t1.true_level_to - t1.true_level_from from tab3 t1;
select t1.id,
trunc(t1.true_level_from) day_,
sum(t1.true_level_to - t1.true_level_from) * 24
from tab3 t1
group by t1.id,
trunc(t1.true_level_from)
;
with tmp as
(select to_Date('20200102092951', 'YYYYMMDDHH24MISS') as from_date,
to_Date('20200102092952', 'YYYYMMDDHH24MISS') as to_date
from dual
union all
select to_Date('20200102103339', 'YYYYMMDDHH24MISS') as from_date,
to_Date('20200102103340', 'YYYYMMDDHH24MISS') as to_date
from dual),
zz as--跨天的拆成两天
(select distinct greatest(from_date,
to_date(to_char(trunc(to_date - level + 1),
'yyyymmdd'),
'yyyymmdd hh24:mi:ss')) from_date,
least(to_date,
to_date(to_char(trunc(to_date - level + 2),
'yyyymmdd'),
'yyyymmdd hh24:mi:ss')) to_date
from tmp
connect by level - 1 <= trunc(to_date) - trunc(from_date)),
xx as
(select min(from_date) over(partition by trunc(from_date)) as min_date,
--一天开始时间
max(to_date) over(partition by trunc(from_date)) as max_date,
--一天结束时间
trunc(from_date) day1,
from_date,
to_date
from zz),
yy as
(select max_date - min_date as minusday,--所占时间,含非运行时间
from_date,
day1,
nvl(max(to_date) over(order by from_date rows between unbounded
preceding and 1 preceding),--提前一个量的最大值,不包含本身(本次修改的核心代码)
from_date) as max_to_date--求上一次结束时间
from xx)
select round((max(minusday) - sum(greatest((from_date - max_to_date), 0))) * 24,--转换为小时
--当前开始时间-上一次结束时间
4),
day1
from yy
group by day1;
with tab0 as(
select 1 rowid_, 1 id, to_date('01 01', 'dd hh24') from_, to_date('01 03', 'dd hh24') to_ from dual union all
select 2 rowid_, 1 id, to_date('01 05', 'dd hh24') from_, to_date('01 08', 'dd hh24') to_ from dual union all
select 3 rowid_, 1 id, to_date('01 05', 'dd hh24') from_, to_date('01 08', 'dd hh24') to_ from dual union all
select 4 rowid_, 1 id, to_date('01 05', 'dd hh24') from_, to_date('01 08', 'dd hh24') to_ from dual union all
select 5 rowid_, 1 id, to_date('01 07', 'dd hh24') from_, to_date('01 10', 'dd hh24') to_ from dual union all
select 6 rowid_, 1 id, to_date('01 09', 'dd hh24') from_, to_date('01 10', 'dd hh24') to_ from dual union all
select 6 rowid_, 1 id, to_date('01 13', 'dd hh24') from_, to_date('01 14', 'dd hh24') to_ from dual union all
select 7 rowid_, 1 id, to_date('01 23', 'dd hh24') from_, to_date('04 08', 'dd hh24') to_ from dual
)
, tab1 as (
select t1.rowid_,
t1.id,
t1.from_,
nvl(t1.to_, sysdate) to_
from tab0 t1
)
, tab2 as (
select t1.*,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.from_)), t1.from_) true_from,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.from_), t1.to_), t1.to_) true_to
from tab1 t1
where 1 = 1
order by t1.from_
)
, tab3 as(
select t1.*,
decode(level, 1, t1.true_from, trunc(t1.true_from) + level - 1) true_level_from,
decode(level, max(level) over(partition by t1.rowid_), true_to, trunc(t1.true_to) - (max(level) over(partition by t1.rowid_) - level - 1)) true_level_to,
level
from tab2 t1
connect by prior t1.rowid_ = t1.rowid_
and prior dbms_random.value is not null
and level <= trunc(t1.true_to) - trunc(t1.true_from) + 1
)
--select t1.*, t1.true_level_to - t1.true_level_from from tab3 t1;
select t1.id,
trunc(t1.true_level_from) day_,
sum(t1.true_level_to - t1.true_level_from)
from tab3 t1
group by t1.id,
trunc(t1.true_level_from)
;
with tab0 as(
select 1 rowid_, 1 id, to_Date('20200102092951', 'YYYYMMDDHH24MISS') from_, to_Date('20200102092952', 'YYYYMMDDHH24MISS') to_ from dual union all
select 2 rowid_, 1 id, to_Date('20200102103339', 'YYYYMMDDHH24MISS') from_, to_Date('20200102103340', 'YYYYMMDDHH24MISS') to_ from dual
)
, tab1 as (
select t1.rowid_,
t1.id,
t1.from_,
nvl(t1.to_, sysdate) to_
from tab0 t1
)
, tab2 as (
select t1.*,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.from_)), t1.from_) true_from,
nvl(greatest(t1.from_, lag(t1.to_) over(partition by t1.id order by t1.from_), t1.to_), t1.to_) true_to
from tab1 t1
where 1 = 1
order by t1.from_
)
, tab3 as(
select t1.*,
decode(level, 1, t1.true_from, trunc(t1.true_from) + level - 1) true_level_from,
decode(level, max(level) over(partition by t1.rowid_), true_to, trunc(t1.true_to) - (max(level) over(partition by t1.rowid_) - level - 1)) true_level_to,
level
from tab2 t1
connect by prior t1.rowid_ = t1.rowid_
and prior dbms_random.value is not null
and level <= trunc(t1.true_to) - trunc(t1.true_from) + 1
)
--select t1.*, t1.true_level_to - t1.true_level_from from tab3 t1;
select t1.id,
trunc(t1.true_level_from) day_,
sum(t1.true_level_to - t1.true_level_from) * 86400
from tab3 t1
group by t1.id,
trunc(t1.true_level_from)
;
with tmp as
(select sysdate - 1.75 as from_date, sysdate - 1 as to_date
from dual
union all
select sysdate - 1.2 from_date, sysdate - 1.1 as to_date
from dual
union all
select sysdate - 1.7 from_date, sysdate - 0.9 as to_date
from dual),
xx as
(select min(from_date) over() as min_date,
max(to_date) over() as max_date,
from_date,
to_date,
row_number() over(order by from_date) as rn
from tmp),
yy as (
select max_date-min_date as minusday,
--max_date,
--min_date,
from_date,
--to_date,
max(to_date) over(order by from_date rows between unbounded preceding and current row) as max_to_date
from xx)
select max(minusday)-sum(greatest((from_date - max_to_date),0))
from yy
with tmp as
(select sysdate - 1.75 as from_date, sysdate - 1 as to_date
from dual
union all
select sysdate - 1.2 from_date, sysdate - 1.1 as to_date
from dual
union all
select sysdate - 1.7 from_date, sysdate - 0.9 as to_date
from dual),
zz as (
select distinct
greatest(from_date,to_date(to_char(trunc(to_date-level+1),'yyyymmdd'),'yyyymmdd hh24:mi:ss')) from_date,
least(to_date,to_date(to_char(trunc(to_date-level+2),'yyyymmdd'),'yyyymmdd hh24:mi:ss')) to_date
from tmp
connect by level-1<=trunc(to_date)-trunc(from_date)),
xx as
(select min(from_date) over(partition by trunc(from_date)) as min_date,
max(to_date) over(partition by trunc(from_date)) as max_date,
trunc(from_date) day1,
from_date,
to_date
from zz),
yy as (
select max_date-min_date as minusday,
from_date,
day1,
max(to_date) over(order by from_date rows between unbounded preceding and current row) as max_to_date
from xx)
select round(max(minusday)-sum(greatest((from_date - max_to_date),0))*24,4),
day1
from yy
group by day1