值更改时获取最后日期

时间:2019-12-07 01:09:20

标签: sql oracle

当流失值更改时,我需要获取最后一个运行日期。我需要返回结果:

> 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  

这是当前的原始数据集:

> 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

流失值的值在11/26/2019从False更改为True。

如果要查询今天的当前运行日期,我需要知道最后一个日期是什么时候流失<>当前流失日期。

我有一个查询可以查询到11/19/2019的信息,但它退出的权利不高。

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 
;

3 个答案:

答案 0 :(得分:1)

您可以利用以下分析功能:

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 :(得分:0)

您希望每一行都有日期,因此您不需要任何过滤。这建议窗口功能:

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;

答案 2 :(得分:0)

Oracle 12.1引入了match_recognize子句,该子句可以非常快速地解决此类问题。这是一种方法。我首先创建一个小的测试表,由两个不同的客户测试该查询在该更通用的情况下是否正常工作。

表创建:

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
;

查询和输出:

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