通过排列ID的分区标识年度快照上的最新记录

时间:2019-04-19 21:13:02

标签: oracle plsql oracle-data-integrator

我具有以下情况,并希望基于“排列ID”,“登录日期”来设置指标。如果用户登录网站在“日历年”中有多个时间,则最近的记录需要设置Y,否则“ N”。我还需要设置指标,例如“下两个”行(也就是行。)(表示在2017年12月13日最近访问的去年的1121221需要设置为“ Y”,如果用户在下一个紧接的1/12/2018访问,则为“ Y”)

enter image description here

1 个答案:

答案 0 :(得分:0)

这是一种选择。它是做什么的?

  • TEST CTE是一些示例行。请注意ARRANGE_ID = 999,其日期为2017年和2019年(这意味着没有连续的年份,因此2019年的日期应为指标“ N”。但是,您没有说,如果存在2019年还有另一个日期;他们两个都将获得“ N”,还是最大登录日期仍将获得“ Y”?
  • INTER CTE使用MAX分析函数查找年份的最大登录日期,并使用LAG分析函数返回前一个登录日期(以便我检查是否那几年是连续的还是不连续的
  • 最终查询使用CASE查找某些行是否满足使指标等于'Y'的条件

您在这里:

SQL> with test (arrange_id, login_date) as
  2    (select 234, date '2017-02-18' from dual union all
  3     select 234, date '2017-04-13' from dual union all
  4     select 234, date '2017-11-14' from dual union all
  5     select 234, date '2018-01-14' from dual union all
  6     select 234, date '2018-09-11' from dual union all
  7     select 234, date '2019-04-02' from dual union all
  8     select 234, date '2019-05-18' from dual union all
  9     select 112, date '2017-02-23' from dual union all
 10     select 112, date '2017-12-13' from dual union all
 11     select 112, date '2018-01-12' from dual union all
 12     select 999, date '2017-01-01' from dual union all
 13     select 999, date '2017-05-25' from dual union all
 14     select 999, date '2019-01-01' from dual
 15    ),
 16  inter as
 17    (select arrange_id,
 18       login_date,
 19       max(login_date) over
 20         (partition by arrange_id, extract (year from login_date)) maxdate,
 21       lag(login_date) over (partition by arrange_id order by login_date) prev_date
 22     from test
 23    )
 24  select arrange_id,
 25    login_date,
 26    case when login_date = maxdate and
 27              extract(year from login_date) - extract(year from prev_date) <= 1 then 'Y'
 28         else 'N'
 29    end indicator
 30  from inter
 31  order by arrange_id, login_date;

ARRANGE_ID LOGIN_DATE I
---------- ---------- -
       112 02/23/2017 N
       112 12/13/2017 Y  -- Y because it is MAX in 2017
       112 01/12/2018 Y  -- Y because it is MAX in 2018 and 2018 follows 2017
       234 02/18/2017 N
       234 04/13/2017 N
       234 11/14/2017 Y  -- Y because it is MAX in 2017
       234 01/14/2018 N
       234 09/11/2018 Y  -- Y because it is MAX in 2018 and 2018 follows 2017
       234 04/02/2019 N
       234 05/18/2019 Y  -- Y because it is MAX in 2019 and 2019 follows 2018
       999 01/01/2017 N
       999 05/25/2017 Y  -- Y because it is MAX in 2017
       999 01/01/2019 N  -- N because it is MAX in 2019, but 2019 doesn't follow 2017

13 rows selected.

SQL>