因此,我创建了一个表,该表包含带有所有客户购买记录的交易表中的以下列:
我正在尝试创建一个表,该表的输出为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
好吧,由于语法错误(没有CASE),您现有的查询将不会运行,否则它在Teradata中有效。
但是有两个问题:
YEAR
或MONTH
添加间隔(可能会导致月末日期的日期无效),请ADD_MONTHS
改用。可以使用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
顺便说一句,没有命名为DBMS SQL
(当然,Microsoft尝试将其与他们的产品相关联)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句