首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >值发生变化时抓取最后一个日期

值发生变化时抓取最后一个日期
EN

Stack Overflow用户
提问于 2019-12-07 09:09:20
回答 3查看 72关注 0票数 0

我需要抓取最后一次运行日期,然后更改客户流失值。我需要返回以下结果:

代码语言:javascript
复制
> customer  billto   banner     rundate        churn    lastchurndate

> 976193     976193   GexPro     12/04/2019     true     11/26/2019
> 976193     976193   GexPro     11/26/2019     true     11/26/2019  
> 976193     976193   GexPro     11/19/2019     false    11/26/2019  

这是当前的原始数据集:

代码语言:javascript
复制
> customer  billto   banner     rundate        churn    

> 976193     976193  GexPro     12/04/2019     true      

>976193     976193   GexPro     11/26/2019     true     

>976193     976193   GexPro     11/19/2019     false

2019年11/26,流失值从False变为True。

如果我要查询截至今天的当前运行日期,我需要知道最后的日期是何时流失<>当前流失日期。

我有一个问题,我可以得到11/19/2019,但它不是正确的退出。

代码语言:javascript
复制
select
    v.st_cust_no
    ,v.bt_cust_no
    ,v.banner
    ,v.rundate
    ,v.churn
    ,case when c."last churn date" is null then v.rundate
        else c."last churn date"
     end as "last churn date" 
    ,c."last churn" 
    from 
     dwstage v
    left join 
    (    
    select 
     z.st_cust_no
    ,z.BT_CUST_NO
    ,z.banner
    ,z.rundate 
    ,z.churn as "current churn" 
    ,x.latest as "last churn date" 
    ,x.churn "last churn" 

    from dwstage    z

        inner join 
        (select 
             st_cust_no
            ,BT_CUST_NO
            ,banner
            ,churn
            ,max(rundate) as latest 
            from dwstage
            group by 
                 st_cust_no
                ,BT_CUST_NO
                ,banner
                ,churn) x

        on z.st_cust_no = x.st_cust_no
        and z.bt_cust_no = x.bt_cust_no
        and z.BANNER = x.BANNER
    where z.churn <> x.churn
    ) c
     on v.st_cust_no = c.st_cust_no
        and v.bt_cust_no = c.bt_cust_no
        and v.BANNER = c.BANNER
        and v.rundate = c.rundate
-- where v.st_cust_no = '14025'
order by 
    v.st_cust_no
   ,v.banner
   ,v.rundate desc 
;
EN

回答 3

Stack Overflow用户

发布于 2019-12-07 10:04:35

您可以按如下方式利用分析函数:

代码语言:javascript
复制
Select * from
  (Select t.*,
          Row_number() over (partition by customer order by rundate desc nulls last) as rn
   from
     (Select t.*, 
             case when lag(chrun) over (partition by cutomer order by rundate) <> chrun then 1 end as chrunchanged
        From your_table t) t
  Where chrunchanged = 1)
Where rn = 1

干杯!!

票数 1
EN

Stack Overflow用户

发布于 2019-12-07 10:59:17

您希望在每一行上显示日期,因此不需要任何过滤。这建议使用窗口函数:

代码语言:javascript
复制
select s.*,
       max(case when prev_churn <> churn then rundate) over (partition by customer, billto) as last_churndate
from (select s.*,
             lag(churn) over (order by customer, billto order by rundate) as prev_churn
      from dwstage s
     ) s;
票数 0
EN

Stack Overflow用户

发布于 2019-12-09 07:13:44

Oracle12.1引入了match_recognize子句,它可以非常快速地解决这类问题。这里有一种方法。我首先创建一个小的测试表,使用两个不同的客户来测试查询在更一般的场景中是否正确工作。

表创建:

代码语言:javascript
复制
create table dwstage (customer, billto,  banner, rundate, churn) as
  select 976193, 976193, 'GexPro', to_date('12/04/2019', 'mm/dd/yyyy'), 'true'  from dual union all
  select 976193, 976193, 'GexPro', to_date('11/26/2019', 'mm/dd/yyyy'), 'true'  from dual union all
  select 976193, 976193, 'GexPro', to_date('11/19/2019', 'mm/dd/yyyy'), 'false' from dual union all
  select 999999, 999999, 'Banner', to_date('03/21/2019', 'mm/dd/yyyy'), 'false' from dual union all
  select 999999, 999999, 'Banner', to_date('04/21/2019', 'mm/dd/yyyy'), 'true'  from dual union all
  select 999999, 999999, 'Banner', to_date('08/03/2019', 'mm/dd/yyyy'), 'false' from dual union all
  select 999999, 999999, 'Other' , to_date('08/31/2019', 'mm/dd/yyyy'), 'false' from dual
;

查询和输出:

代码语言:javascript
复制
select customer, billto, banner, rundate, churn, lastchurndate
from   dwstage
match_recognize(
  partition by customer
  order     by rundate desc
  measures  final last(a.rundate) as lastchurndate
  all rows  per match
  pattern   (^ a+ b*)
  define    a as churn = first(churn)
)
;


  CUSTOMER     BILLTO BANNER RUNDATE    CHURN LASTCHURND
---------- ---------- ------ ---------- ----- ----------
    976193     976193 GexPro 12/04/2019 true  11/26/2019
    976193     976193 GexPro 11/26/2019 true  11/26/2019
    976193     976193 GexPro 11/19/2019 false 11/26/2019
    999999     999999 Other  08/31/2019 false 08/03/2019
    999999     999999 Banner 08/03/2019 false 08/03/2019
    999999     999999 Banner 04/21/2019 true  08/03/2019
    999999     999999 Banner 03/21/2019 false 08/03/2019
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59222159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档