有2张桌子
--Результат подзапроса (отфильтрованных клиентов)
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
--Вся таблица
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-03-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
Суть задачи
- 查明客户档案是否在 3 个月前创建。
Важно
- 查看问卷建立之日起 3 个月前(针对每个客户)。
此外,该表fq
是子查询的结果,因此在表中搜索时,t
不能考虑重复的配置文件。
我试图用以下逻辑解决:
- 有必要从请求中找到客户输入问卷的最后日期
fq
- 附表
t
以按其搜索 - 从客户的最后日期开始,您需要查看,也许还有其他 3 个月的个人资料。
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
1 1 25-MAY-21
2 NULL NULL
5 1 14-JAN-21
5 1 12-JAN-21
事实上db<>fiddle
我会这样做:
结果(在db<>fiddle上):
问题中的预期输出可能与任务描述不同。