循环遍历不同 'consumers' 到 return 日期之间天数的记录

Looping through records for different 'consumers' to return number of days between dates

我一直在查看有关遍历记录的问题,但找不到任何可以解决我问题的方法

所以我有 table 的消费者参与活动。 profile_id 特定于一个消费者,这里我们有两个消费者。我们看到他们输入的广告系列和输入日期,以及一个计数告诉我们它是哪个条目(按时间顺序)

profile_id    campaign   create_date    entry      
74704338      2320        28-07-2015      1
74704338      2388        28-01-2016      2
74704338      2464        29-04-2016      3
74704338      2476        03-05-2016      4
74704338      2505        25-05-2016      5
81990916      2320        05-11-2015      1
81990916      2388        22-01-2016      2
81990916      2464        28-04-2016      3
81990916      2467        28-04-2016      4
81990916      2434        02-05-2016      5

我想要做的是 运行 通过每个配置文件(消费者)的每条记录并获得最大值。第 n 条和第 n+1 条之间的天数,以及与之关联的活动。

因此对于 profile_id 74704338 我们应该得到以下结果,因为 1 和 2 之间的天数是序列中最大的,而活动 2388 是他们在这段时间过去后进入的活动

profile_id    campaign   num_days
74704338      2388       184

同样对于 81990916 我们应该得到

profile_id    campaign   num_days
81990916      2464       97

我想我会使用声明语句来执行此操作,但不知道从哪里开始。非常感谢您的帮助

非常感谢

解析函数就是为这种练习而生的。首先我们计算差异(使用 lag() 函数),然后计算每个 "partition" 内的最大差异 profile_id,然后在最后一个(最外层)查询中我们 select差异等于最大值的行。

我使用 Oracle 11.2 及更高版本的语法编写了这篇文章;对于早期版本,它可以通过在子查询定义中(而不是在它们的声明中)创建列别名来重写。

with
     test_data ( profile_id, campaign, create_date ) as (      
       select 74704338, 2320, to_date('28-07-2015', 'dd-mm-yyyy') from dual union all
       select 74704338, 2388, to_date('28-01-2016', 'dd-mm-yyyy') from dual union all
       select 74704338, 2464, to_date('29-04-2016', 'dd-mm-yyyy') from dual union all
       select 74704338, 2476, to_date('03-05-2016', 'dd-mm-yyyy') from dual union all
       select 74704338, 2505, to_date('25-05-2016', 'dd-mm-yyyy') from dual union all
       select 81990916, 2320, to_date('05-11-2015', 'dd-mm-yyyy') from dual union all
       select 81990916, 2388, to_date('22-01-2016', 'dd-mm-yyyy') from dual union all
       select 81990916, 2464, to_date('28-04-2016', 'dd-mm-yyyy') from dual union all
       select 81990916, 2467, to_date('28-04-2016', 'dd-mm-yyyy') from dual union all
       select 81990916, 2434, to_date('02-05-2016', 'dd-mm-yyyy') from dual
     ),
     diffs ( profile_id, campaign, create_date, diff ) as (
       select profile_id, campaign, create_date,
              create_date - lag(create_date) over (partition by profile_id 
                                                   order by create_date)
       from   test_data
     ),
     with_max ( profile_id, campaign, create_date, diff, max_diff ) as (
       select profile_id, campaign, create_date, diff,
              max(diff) over (partition by profile_id)
       from   diffs
     )
select profile_id, campaign, create_date, diff
from   with_max
where  diff = max_diff
;

PROFILE_ID  CAMPAIGN CREATE_DATE  DIFF
---------- --------- ----------- -----
  74704338      2388 28-01-2016    184
  81990916      2464 28-04-2016     97

2 rows selected.

这是获取记录的另一种方式。成本是 5。mathguy 答案成本是 22。

select *
  from (select *
          from (select profile_id,
                       campaign_id,create_date,
                       (create_date - lag(create_date, 1, create_date)
                        OVER(PARTITION BY profile_id ORDER BY ENTRY
                             NULLS LAST)) nod
                  from campaign)
         order by nod desc)
 where rownum <= 2