with TEST76ES(
"OBID",
"YEAR",
"EQID",
"START_DATE_FACT",
"END_DATE_FACT",
"REPAIR_T"
) as (
select 1,201501,11, to_date('2015-01-05', 'YYYY-MM-DD'),to_date('2015-01-18', 'YYYY-MM-DD'),'remont_1' from dual union all
select 1,201501,22, to_date('2015-01-25', 'YYYY-MM-DD'),to_date('2015-01-30', 'YYYY-MM-DD'),'remont_1' from dual union all
select 1,201505,33, to_date('2015-05-05', 'YYYY-MM-DD'),to_date('2015-05-14', 'YYYY-MM-DD'),'remont_3' from dual union all
select 1,201608,22, to_date('2016-08-07', 'YYYY-MM-DD'),to_date('2016-08-16', 'YYYY-MM-DD'),'remont_2' from dual union all
select 2,201503,77, to_date('2015-03-05', 'YYYY-MM-DD'),to_date('2015-03-14', 'YYYY-MM-DD'),'remont_12' from dual union all
select 3,201704,25, to_date('2017-04-10', 'YYYY-MM-DD'),to_date('2017-04-16', 'YYYY-MM-DD'),'remont_6' from dual union all
select 3,201602,66, to_date('2016-02-10', 'YYYY-MM-DD'),to_date('2016-02-16', 'YYYY-MM-DD'),'remont_66' from dual
)
select
substr(t1.YEAR, 1, 4) as year,
substr(t1.YEAR, 5, 2) as month_id,
t1.OBID,
t2.EQID
from
TEST76ES t1
inner join TEST76ES t2
on t2.OBID = t1.OBID
and substr(t2.YEAR, 1, 4) = substr(t1.YEAR, 1, 4)
where 1 = 1
and t1.OBID = 1
group by
substr(t1.YEAR, 1, 4),
substr(t1.YEAR, 5, 2),
t1.OBID,
t2.EQID
|