【问题标题】:Running Count within Date range日期范围内的运行计数
【发布时间】:2020-07-05 00:14:12
【问题描述】:

在 Teradata 中,我试图获取指定日期范围内的运行总数。我需要的是找出哪些客户在 10 天内提出了 5 次或更多次索赔。

样本数据: Claim_ID Claim_Dt Cust_num

15087   1/1/2020    123000
15099   2/3/2020    123000
18473   2/8/2020    123000
18476   2/8/2020    123000
18488   2/10/2020   123000
15080   1/1/2020    133000
15082   1/1/2020    133000
18555   2/13/2020   133000
18588   2/15/2020   133000
15601   2/16/2020   133000
15711   2/18/2020   133000
15799   2/21/2020   133000
15816   2/22/2020   133000
15926   2/27/2020   133000
15988   3/1/2020    133000

预期结果:

Cust_num   Claim_Count   Min_date   Max Date
133000           6        2/13/2020   2/22/2020

这是我目前使用 LAG 函数的代码:

select  CLAIM_DT, CUST_NUM,      
ROW_NUMBER() OVER (PARTITION BY CUST_NUM ORDER BY CUST_NUM, CLAIM_DT) as     ROW_ID,

LAG(claim_dt,1) OVER(partition by cust_num order by claim_dt) as datediff,
claim_dt -datediff as DAYS_BETWEEN,
COUNT(claim_id) OVER(
PARTITION BY Cust_Num 
ORDER BY claim_dt
RESET WHEN DAYS_BETWEEN > 10
ROWS BETWEEN 5 PRECEDING AND 5 FOLLOWING 
  ) AS Claims_count, CLAIM_ID
from       (       
select  CLAIM_ID, CLAIM_DT, CUST_NUM
from    Claims_CUST_STILL_OPEN 

   ) as dt 

QUALIFY Claims_count >= 5
order by 2,1,3

我能够得到 min(claim_dt) 和 max(claim_dt) 之间的简单计数,但不知道如何获得运行计数。

我相信我需要一个使用 UNBOUNDED PRECEDING 行的 RESET 函数,但无法让它工作。

任何帮助将不胜感激。

【问题讨论】:

    标签: date count teradata-sql-assistant


    【解决方案1】:

    如果您想了解哪些客户在任何 10 天内提出了 5 次以上的索赔,请使用以下方法:

    SELECT 
      Cust_Num, 
      COUNT(t.claim_id) OVER(
        PARTITION BY t.Cust_Num 
        ORDER BY c.calendar_date
        ROWS BETWEEN 4 PRECEDING AND 5 FOLLOWING -- count # claims in 10-day window
      ) AS moving_count
    FROM sys_calendar.calendar c -- Seed result set with range of days
    LEFT JOIN my_table t ON c.calendar_date = t.claim_dt -- Join customer data
    WHERE c.calendar_date BETWEEN <start_date> AND <end_date>
    QUALIFY moving_count >= 5 -- Only get rows with 5+ claims
    

    您可能无法在主查询中包含DISTINCT,因此要获得cust_num 值的唯一列表,您可以执行外部SELECT DISTINCT cust_num FROM (...) 并将上面的查询放在(...) 中。

    【讨论】:

    • 这似乎没有得到我真正需要的东西(尽管很感激)。我需要一种方法来重置日期范围的计数。虽然这似乎让我对索赔进行了计数,但它并没有让我能够从每个新日期重新设置。我认为问题在于需要在 10 天后使用 RESET 功能。
    • “重置日期范围的计数”是什么意思?请在您的原始帖子中添加一些示例预期输出。
    • 我添加了预期的结果。我也正在使用 LAG 功能进行可能的计数,如果我让它工作,我会分享。
    【解决方案2】:

    首先展开每个索赔行以覆盖有效的 10 天窗口(创建该行的 10 个副本),然后汇总并过滤结果。外部的 GROUP BY 是为每个 min_dt 只给出一行(最大计数/最新 max_dt)。

    select cust_id, max(claim_ct) as claim_ct, min_dt, max(max_dt) as max_ct
    FROM (
      select cust_id, count(*) as claim_ct, min(claim_dt) as min_dt, max(claim_dt) as max_dt
      FROM (
         select claim_id, claim_dt, cust_id, begin(window_pd) as window_dt
         from claims
         expand on period(claim_dt, claim_dt+10) as window_pd 
           ) expand_effective_date_range
      group by cust_id, window_dt /* running total by customer & day */ 
      having window_dt = max_dt /* only keep rows with matching claim dates */
      and Claim_ct >=5 /* and then only if count is at least 5 */
         ) summarize_and_filter
    group by cust_id, min_dt; 
    

    【讨论】:

    • 这很棒。我认为我在延迟功能和重置的使用上偏离了轨道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多