RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1291905
Accepted
Akalit
Akalit
Asked:2022-06-05 03:13:11 +0000 UTC2022-06-05 03:13:11 +0000 UTC 2022-06-05 03:13:11 +0000 UTC

每行按日期搜索

  • 772


有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不能考虑重复的配置文件。

我试图用以下逻辑解决:

  1. 有必要从请求中找到客户输入问卷的最后日期fq
  2. 附表t以按其搜索
  3. 从客户的最后日期开始,您需要查看,也许还有其他 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

sql
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. 0xdb
    2022-06-05T17:39:31Z2022-06-05T17:39:31Z

    我会这样做:

    select 
        fq.*, case 
              when add_months (fq.datenter, -3) <= prevFormIssued 
              then 'y' else 'n' end existLessAs3MonthOld
    from fq
    cross apply (
        select max (datenter) keep (dense_rank first order by datenter desc) prevFormIssued
        from t 
        where t.idclient = fq.idclient 
        and t.datenter < fq.datenter)
    

    结果(在db<>fiddle上):

      IDCLIENT    IDBLANK DATENTER            E
    ---------- ---------- ------------------- -
             1          1 2021-06-03 12:00:00 y
             5          4 2021-03-13 12:00:00 y
             2          1 2021-04-03 12:00:00 n
    

    问题中的预期输出可能与任务描述不同。

    • 5
  2. Best Answer
    Viktorov
    2022-06-05T04:45:32Z2022-06-05T04:45:32Z
    SELECT t1.idclient
          ,CASE
             WHEN t2.idclient IS NOT NULL THEN
              1
             ELSE
              NULL
           END AS is_enter_earlier -- 1 если заводилась ранее
          ,t2.datenter
      FROM fq t1
      LEFT JOIN t t2
        ON t1.idclient = t2.idclient
       AND t1.datenter != t2.datenter
       AND t2.datenter BETWEEN ADD_MONTHS(t1.datenter, -3) AND t1.datenter
     ORDER BY t1.idclient;
    
    • 3

相关问题

  • 通过 OUT 参数从过程结果输出

  • ON 关键字附近的语法错误 - SQL

  • 多表查询中的 Count() 聚合函数

  • 根据时间更改单元格中的日期

  • phpMyAdmin 中的错误 #1064 SQL 查询

  • Qt:包含变量的数据库查询

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5