次日、三日、七日用户留存SQL代码
SELECT 首次下单日期
,COUNT(DISTINCT bussinessid) AS 下单商家数
,COUNT(DISTINCT CASE WHEN 首单间隔天数="1" THEN bussinessid END) AS 第2日下单数
,COUNT(DISTINCT CASE WHEN 首单间隔天数="2" THEN bussinessid END) AS 第3日下单数
,COUNT(DISTINCT CASE WHEN 首单间隔天数="6" THEN bussinessid END) AS 第7日下单数
,COUNT(DISTINCT CASE WHEN 首单间隔天数="14" THEN bussinessid END) AS 第15日下单数
,COUNT(DISTINCT CASE WHEN 首单间隔天数="29" THEN bussinessid END) AS 第30日下单数
FROM (
SELECT bussinessid
,createdate
,首次下单日期
,DATEDIFF(createdate,首次下单日期) 首单间隔天数
FROM (
SELECT bussinessid
,createdate
,MIN(createdate) OVER(PARTITION BY bussinessid ORDER BY createdate) AS 首次下单日期
FROM ((SELECT DISTINCT bussinessid
,SUBSTR(createtime,1,10) AS createdate
FROM maxcomptue.dws_order_demo
WHERE pt = REPLACE(SUBSTR(DATEADD(GETDATE(), - 1, 'dd'),1,10),'-','')
AND orderchannel = 0
ORDER BY createdate)a
LEFT JOIN
(SELECT id,role --判断个人/商家
FROM maxcomptue.dwd_cust_user_info
WHERE pt = REPLACE(SUBSTR(DATEADD(GETDATE(),-1, 'dd'),1,10),'-',''))b
ON a.bussinessid = b.id
)
WHERE ROLE = '1'
GROUP BY bussinessid
,createdate
)
)
GROUP BY 首次下单日期
;