【问题标题】:How to by pass case not when exists in teradata?teradata中不存在时如何绕过案例?
【发布时间】:2016-07-21 16:47:39
【问题描述】:

所以我创建了一个表,其中包含来自所有客户购买记录的事务表的以下列:

  1. 月-年,
  2. 客户 ID、
  3. 当月的交易次数。

我正在尝试创建一个输出为 1.Month-Year、2.该月流失客户数的表,定义为拥有过去 12 个月内没有交易。 (因此,如果客户在 2014 年 1 月只购买了一次,那么他们会在 2015 年 2 月流失。

如果该人在 2015 年 3 月进行了交易,但直到 2016 年 5 月才进行交易,那么他们在 2016 年 4 月再次流失)。

如果有任何建议,我将不胜感激。


我编写的代码在SQL 中有效,但在Teradata 中无效:

select 
month_start_date, 

    (select  1 
     from merchantengagement1 t2 
      where 
          t2.month_start_date >= t.month_start_date - INTERVAL '1' YEAR and
              t2.month_start_date < t.month_start_date and 
              transactions > 0  and 
              t.rcvr_ID = t2.rcvr_ID
    ) then 1 else 0 end) as churnedCustomers
from 
merchantengagement1  t
group by month_start_date

【问题讨论】:

    标签: teradata


    【解决方案1】:

    好吧,由于语法错误(没有 CASE),您现有的查询将不会运行,否则它在 Teradata 中有效。

    但是有两个问题:

    • 在添加间隔时切勿使用YEAR 或MONTH(这可能会导致月末日期的日期无效),请改用ADD_MONTHS。
    • 像这样的相关子查询在所有 DBMS 中都很糟糕,但在 Teradata 中尤其糟糕,会导致产品连接。

    你的逻辑可以用 OLAP 函数来表达,检查以下交易是否提前 12 个月以上或最近一次交易是否超过 12 个月前:

    SELECT rcvr_ID,
       -- if this date is before the next transaction it's a churn
       ADD_MONTHS(month_start_date, 12) AS churn_date
    FROM merchantengagement1
    WHERE transactions > 0
    QUALIFY -- more than 12 months difference
       churn_date <
       COALESCE(MAX(month_start_date) -- next transaction
                OVER (PARTITION BY rcvr_ID
                      ORDER BY month_start_date
                      ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
               , CURRENT_DATE)        -- or today
    

    顺便说一句,没有名为 SQL 的 DBMS(当然,微软试图将其与他们的产品相关联)

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2013-12-28
      相关资源
      最近更新 更多