现有用户行为表,包含设备id、账号id 、用户行为、行为发生时间、进程id四个字段。其中用户行为包含:点击下单按钮(click_order_button) 、打开提交订单页 (open_submit_order_page)、点击提交订单按钮(click_submit_order_button) 、打开支付页(open_payment_page) 、点击确认支付(click_confirm_payment) 、跳转支付成功页面(open_payment_success_page)。 在每个阶段,都有用户的流失。请用sql计算该产品漏斗的数据(即每个步骤的用户留存量和留存率)以及每个步骤间的用户平均停留时间。 注意:设备ID和账号ID是多对多的关系。 drop table if exists dwd_user_behavior; CREATE EXTERNAL TABLE dwd_user_behavior ( device_id STRING COMMENT '设备ID', account_id STRING COMMENT '账号ID', action_id STRING COMMENT '行为类型ID', action_time STRING COMMENT '行为时间', pid STRING COMMENT '进程id' ) COMMENT '用户行为表' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION 'hdfs://hadoop01:8020/test/dwd_user_behavior' ;
时间: 2024-04-09 14:29:01 浏览: 198
您好!根据您提供的用户行为表,以下是用SQL计算产品漏斗数据的示例代码:
1. 计算每个步骤的用户留存量和留存率:
```sql
-- 点击下单按钮(click_order_button) 的用户留存量和留存率
SELECT COUNT(DISTINCT device_id) AS retention_count,
COUNT(DISTINCT device_id) / COUNT(DISTINCT device_id) OVER () AS retention_rate
FROM dwd_user_behavior
WHERE action_id = 'click_order_button';
-- 打开提交订单页 (open_submit_order_page) 的用户留存量和留存率
SELECT COUNT(DISTINCT device_id) AS retention_count,
COUNT(DISTINCT device_id) / COUNT(DISTINCT device_id) OVER () AS retention_rate
FROM dwd_user_behavior
WHERE action_id = 'open_submit_order_page';
-- 点击提交订单按钮(click_submit_order_button) 的用户留存量和留存率
SELECT COUNT(DISTINCT device_id) AS retention_count,
COUNT(DISTINCT device_id) / COUNT(DISTINCT device_id) OVER () AS retention_rate
FROM dwd_user_behavior
WHERE action_id = 'click_submit_order_button';
-- 打开支付页(open_payment_page) 的用户留存量和留存率
SELECT COUNT(DISTINCT device_id) AS retention_count,
COUNT(DISTINCT device_id) / COUNT(DISTINCT device_id) OVER () AS retention_rate
FROM dwd_user_behavior
WHERE action_id = 'open_payment_page';
-- 点击确认支付(click_confirm_payment) 的用户留存量和留存率
SELECT COUNT(DISTINCT device_id) AS retention_count,
COUNT(DISTINCT device_id) / COUNT(DISTINCT device_id) OVER () AS retention_rate
FROM dwd_user_behavior
WHERE action_id = 'click_confirm_payment';
-- 跳转支付成功页面(open_payment_success_page) 的用户留存量和留存率
SELECT COUNT(DISTINCT device_id) AS retention_count,
COUNT(DISTINCT device_id) / COUNT(DISTINCT device_id) OVER () AS retention_rate
FROM dwd_user_behavior
WHERE action_id = 'open_payment_success_page';
```
2. 计算每个步骤间的用户平均停留时间:
```sql
-- 点击下单按钮(click_order_button) 到打开提交订单页(open_submit_order_page) 的平均停留时间
SELECT AVG(DATEDIFF(action_time, LAG(action_time) OVER (ORDER BY action_time)) AS average_stay_time
FROM dwd_user_behavior
WHERE action_id IN ('click_order_button', 'open_submit_order_page');
-- 打开提交订单页(open_submit_order_page) 到点击提交订单按钮(click_submit_order_button) 的平均停留时间
SELECT AVG(DATEDIFF(action_time, LAG(action_time) OVER (ORDER BY action_time)) AS average_stay_time
FROM dwd_user_behavior
WHERE action_id IN ('open_submit_order_page', 'click_submit_order_button');
-- 点击提交订单按钮(click_submit_order_button) 到打开支付页(open_payment_page) 的平均停留时间
SELECT AVG(DATEDIFF(action_time, LAG(action_time) OVER (ORDER BY action_time)) AS average_stay_time
FROM dwd_user_behavior
WHERE action_id IN ('click_submit_order_button', 'open_payment_page');
-- 打开支付页(open_payment_page) 到点击确认支付(click_confirm_payment) 的平均停留时间
SELECT AVG(DATEDIFF(action_time, LAG(action_time) OVER (ORDER BY action_time)) AS average_stay_time
FROM dwd_user_behavior
WHERE action_id IN ('open_payment_page', 'click_confirm_payment');
-- 点击确认支付(click_confirm_payment) 到跳转支付成功页面(open_payment_success_page) 的平均停留时间
SELECT AVG(DATEDIFF(action_time, LAG(action_time) OVER (ORDER BY action_time)) AS average_stay_time
FROM dwd_user_behavior
WHERE action_id IN ('click_confirm_payment', 'open_payment_success_page');
```
请根据实际需求和数据结构调整以上代码,并将`dwd_user_behavior`替换为实际的表名。
阅读全文