数据仓库服务 GAUSSDB(DWS)-漏斗和留存函数:示例
示例
创建表funnel_test:
CREATE TABLE IF NOT EXISTS funnel_test ( user_id INT , event_type TEXT, event_time TIMESTAMP, event_timez TIMESTAMP WITH TIME ZONE, event_time_int BIGINT );
插入数据:
INSERT INTO funnel_test VALUES (1,'浏览页面','2021-01-31 11:00:00', '2021-01-31 11:00:00+08', 10), (1,'点击商品','2021-01-31 11:10:00', '2021-01-31 11:10:00+07', 20), (1,'加入购物车','2021-01-31 11:20:00', '2021-01-31 11:20:00+06', 30), (1,'支付货款','2021-01-31 11:30:00', '2021-01-31 11:30:00+05', 40), (2,'加入购物车','2021-01-31 11:00:00', '2021-01-31 11:00:00+08', 11), (2,'支付货款','2021-01-31 11:10:00', '2021-01-31 11:10:00+08', 12), (1,'浏览页面','2021-01-31 11:00:00', '2021-01-31 11:00:00+01', 50), (3,'浏览页面','2021-01-31 11:20:00', '2021-01-31 11:20:00-04', 30), (3,'点击商品','2021-01-31 12:00:00', '2021-01-31 12:00:00-04', 80), (4,'浏览页面','2021-01-31 11:50:00', '2021-01-31 11:50:00-01', 1000), (4,'支付货款','2021-01-31 12:00:00', '2021-01-31 12:00:00-02', 900), (4,'加入购物车','2021-01-31 12:00:00', '2021-01-31 12:00:00-03', 1001), (4,'点击商品','2021-01-31 12:00:00', '2021-01-31 12:00:00-04', 1001), (5,'浏览页面','2021-01-31 11:50:00', '2021-01-31 11:50:00+08', NULL), (5,'点击商品','2021-01-31 12:00:00', '2021-01-31 12:00:00+08', 776), (5,'加入购物车','2021-01-31 11:10:00', '2021-01-31 11:10:00+08', 999), (6,'浏览页面','2021-01-31 11:50:00', '2021-01-31 11:50:00+01', -1), (6,'点击商品','2021-01-31 12:00:00', '2021-01-31 12:00:00+02', -2), (6,'加入购物车','2021-01-31 12:10:00', '2021-01-31 12:00:00+03', -3);
计算每个用户的漏斗情况。返回结果如下,其中level=0表示用户在窗口期内匹配最大事件深度为0,level=1表示用户在窗口期内匹配最大事件深度为1:
SELECT user_id, windowFunnel( 0, 'default', event_timez, event_type = '浏览页面', event_type = '点击商品', event_type = '加入购物车', event_type = '支付货款' ) AS level FROM funnel_test GROUP BY user_id ORDER by user_id; user_id | level ---------+------- 1 | 1 2 | 0 3 | 1 4 | 1 5 | 1 6 | 1 (6 rows)
计算每个用户的漏斗情况,指定滑动的时间窗口的大小为NULL,返回报错:
SELECT user_id, windowFunnel( NULL, 'default', event_time, event_type = '浏览页面', event_type = '点击商品', event_type = '加入购物车', event_type = '支付货款' ) AS level FROM funnel_test GROUP BY user_id ORDER by user_id; ERROR: Invalid parameter : window length or mode is null.
计算每个用户的漏斗情况,指定多个条件:
SELECT user_id, windowFunnel( 40, 'default', date(event_time), true, true, false, true ) AS level FROM funnel_test GROUP BY user_id ORDER by user_id; user_id | level ---------+------- 1 | 2 2 | 2 3 | 2 4 | 2 5 | 2 6 | 2 (6 rows)
分析用户的留存情况:
SELECT user_id, retention( event_type = '浏览页面', event_type = '点击商品', event_type = '加入购物车', event_type = '支付货款' ) AS r FROM funnel_test GROUP BY user_id ORDER BY user_id ASC; user_id | r ---------+----------- 1 | {1,1,1,1} 2 | {0,0,0,0} 3 | {1,1,0,0} 4 | {1,1,1,1} 5 | {1,1,1,0} 6 | {1,1,1,0} (6 rows)
分析用户的留存情况,指定第一个时间为false:
SELECT user_id, retention( false, event_type = '浏览页面', event_type = '点击商品', event_type = '加入购物车', event_type = '支付货款' ) AS r FROM funnel_test GROUP BY user_id ORDER BY user_id ASC; user_id | r ---------+------------- 1 | {0,0,0,0,0} 2 | {0,0,0,0,0} 3 | {0,0,0,0,0} 4 | {0,0,0,0,0} 5 | {0,0,0,0,0} 6 | {0,0,0,0,0} (6 rows)
分析用户的留存情况总和:
SELECT sum(r[1]), sum(r[2]), sum(r[3]), sum(r[4]) FROM ( SELECT retention(event_type = '浏览页面', event_type = '点击商品', event_type = '加入购物车', event_type = '支付货款') AS r FROM funnel_test GROUP BY user_id ); sum | sum | sum | sum -----+-----+-----+----- 5 | 5 | 4 | 2 (1 row)
统计每个用户在1,3,7天后的付费留存率:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SELECT user_id, range_retention_count(event_type = '浏览页面', event_type = '支付货款', DATE(event_time), ARRAY[1, 3, 7], 'day') as r FROM funnel_test GROUP BY user_id ORDER BY user_id; user_id | r ---------+------------------ 1 | {80135499808768} 2 | {} 3 | {80135499808768} 4 | {80135499808768} 5 | {80135499808768} 6 | {80135499808768} (6 rows) |
- 数据仓库服务GaussDB(DWS)_SQL on Anywhere
- DWS安全_数据仓库服务安全_DWS数据安全管理_DWS安全保障_DWS安全策略
- 调用GaussDB(DWS) API接口_数据仓库服务调用API_如何调用API_在DWS中调用API
- DWS产品介绍_DWS产品优势_DWS功能_DWS使用场景_DWS是什么
- GaussDB数据库函数_GaussDB函数和操作符_高斯数据库函数-华为云
- GaussDB(DWS)常用SQL_常用SQL命令_SQL语法
- GAUSS(DWS)工具_gsql工具_DataStudio工具_DSC工具
- DWS资源管理_GaussDB(DWS)资源管理作用_DWS资源管控
- GaussDB(DWS)服务_什么是IoT数仓_如何使用IoT数仓
- GaussDB函数_GaussDB数据库函数_高斯数据库函数_华为云