0
    CUST_ID     ACCT_ID         FICO_SCORE  DATE_APPLICATION_RECEIVED
1   48378281    2,200,263,271   577         5/3/2001
2   48378281    2,346,832,797   611         5/14/2003
3   48378281    2,210,263,271   560         5/2/2002
4   48378281    2,416,532,797   575         5/14/2005

假设我有一份在我公司的 teradata 中拥有多个帐户的客户列表,我有他们的 fico 分数,以及他们向我们申请新帐户的日期。一个客户必须拥有 2 个以上的帐户,并且最多可以拥有 5 个帐户。以上是该表仅适用于一位客户的外观。我现在如何将这个人群划分为那些在他们的第一个开立账户和第二个开立账户之间的开户日期至少相隔 6 个月的客户?最后我只需要 count(unique(cust_id)) 。

4

1 回答 1

1

如果我理解正确,您只关心前两个帐户至少相隔 6 个月?

SELECT COUNT(*) 
FROM
 (
   SELECT CUST_ID
   FROM tab
   QUALIFY  -- only the first account of a customer
      ROW_NUMBER() 
      OVER (PARTITION BY CUST_ID
            ORDER BY DATE_APPLICATION_RECEIVED) = 1
   AND -- at least six months between this and the next row
      ADD_MONTHS(DATE_APPLICATION_RECEIVED, 6) 
       < MIN(DATE_APPLICATION_RECEIVED) 
         OVER (PARTITION BY CUST_ID
               ORDER BY DATE_APPLICATION_RECEIVED
               ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
 ) AS dt
于 2013-07-13T09:11:00.420 回答