By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select * from V$VERSION;
BANNER | BANNER_FULL | BANNER_LEGACY | CON_ID |
---|---|---|---|
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production | Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production Version 18.4.0.0.0 |
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production | 0 |
--Результат подзапроса (отфильтрованных клиентов)
create table fq (idclient, idblank, datenter) as
select 1, 1, date'2021-06-03'+0.5 from dual union all
select 5, 4, date'2021-03-13'+0.5 from dual union all
select 2, 1, date'2021-04-03'+0.5 from dual
3 rows affected
--Вся таблица
create table t (idclient, idblank, datenter) as
select 5, 13, date'2021-01-14'+0.5 from dual union all
select 1, 1, date'2021-06-03'+0.5 from dual union all
select 1, 5, date'2021-02-13'+0.5 from dual union all
select 2, 14, date'2021-06-03'+0.5 from dual union all
select 3, 12, date'2021-02-17'+0.5 from dual union all
select 4, 9, date'2021-04-03'+0.5 from dual union all
select 5, 6, date'2021-01-12'+0.5 from dual union all
select 6, 7, date'2021-04-03'+0.5 from dual union all
select 5, 4, date'2021-01-13'+0.5 from dual union all
select 3, 3, date'2021-04-03'+0.5 from dual union all
select 4, 2, date'2021-02-13'+0.5 from dual union all
select 2, 1, date'2021-04-03'+0.5 from dual union all
select 1, 10, date'2021-05-25'+0.5 from dual
13 rows affected
select t2.idclient, case when t1.idblank is not null then 1 end as priz_m3
,t1.datenter
from (select
idclient, idblank, datenter
from t ) t1
left join (select
t.idclient, max(t.datenter) as datenter
from t join fq on t.idclient=fq.idclient
group by t.idclient
) t2 on t1.idclient=t2.idclient
and t2.datenter between add_months(t1.datenter,-3) and t1.datenter-1
IDCLIENT | PRIZ_M3 | DATENTER |
---|---|---|
null | 1 | 03-APR-21 |
null | 1 | 03-JUN-21 |
null | 1 | 03-APR-21 |
null | 1 | 03-APR-21 |
null | 1 | 13-FEB-21 |
null | 1 | 14-JAN-21 |
null | 1 | 12-JAN-21 |
null | 1 | 13-JAN-21 |
null | 1 | 17-FEB-21 |
null | 1 | 03-APR-21 |
null | 1 | 03-JUN-21 |
null | 1 | 13-FEB-21 |
null | 1 | 25-MAY-21 |