首页 > 试题广场 >

统计用户从访问到下单的转化率

[编程题]统计用户从访问到下单的转化率
  • 热度指数:38487 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现有某个商城部分订单数据,用户访问数据,如下所示:
订单信息表:order_tb(订单id:order_id,用户id:user_id,订单金额:order_price,订单创建时间:order_time)
order_id user_id order_price order_time
101 11 380 2022-09-01 09:00:00
102 12 200 2022-09-01 10:00:00
103 13 260 2022-09-01 12:00:00
104 11 100 2022-09-02 11:00:00
105 12 150 2022-09-02 12:00:00
106 12 1200 2022-09-02 13:00:00
107 11 60 2022-09-03 09:00:00
108 13 380 2022-09-03 09:30:00
访问信息表:visit_tb(访问信息id:info_id,用户id:user_id,访问时间:visit_time,离开时间:leave_time
info_id user_id visit_time leave_time
911 10 2022-09-01 08:00:00 2022-09-01 09:02:00
912 11 2022-09-01 08:30:00 2022-09-01 09:10:00
913 12 2022-09-01 09:50:00 2022-09-01 10:12:00
914 13 2022-09-01 11:40:00 2022-09-01 12:22:00
921 11 2022-09-02 10:30:00 2022-09-02 11:05:00
922 11 2022-09-02 12:00:00 2022-09-02 12:02:00
923 12 2022-09-02 11:40:00 2022-09-02 13:15:00
924 13 2022-09-02 09:00:00 2022-09-02 09:02:00
925 14 2022-09-02 10:00:00 2022-09-02 10:40:00
931 10 2022-09-03 09:00:00 2022-09-03 09:22:00
932 11 2022-09-03 08:30:00 2022-09-03 09:10:00
933 13 2022-09-03 09:00:00 2022-09-03 09:32:00
请统计该商城每天用户从访问到下订单的转化率。
要求输出:日期,转化率(该日下订单人数/访问人数,以百分数形式输出并四舍五入保留1位小数)
注:输出结果按照日期升序排序;
示例数据结果如下:
date cr
2022-09-01 75.0%
2022-09-02 50.0%
2022-09-03 66.7%

结果解释:
以2022-09-01为例,该日共计有user_id为10、11、12、13共计4名用户访问商城,
其中11、12、13共计3名用户下了订单,故转化率为3/4=75.0%;
其他结果同理。
示例1

输入

drop table if exists  `order_tb` ; 
CREATE TABLE `order_tb` (
`order_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`order_price` int(11) NOT NULL,
`order_time` datetime NOT NULL,
PRIMARY KEY (`order_id`));
INSERT INTO order_tb VALUES(101,11,380,'2022-09-01 09:00:00'); 
INSERT INTO order_tb VALUES(102,12,200,'2022-09-01 10:00:00'); 
INSERT INTO order_tb VALUES(103,13,260,'2022-09-01 12:00:00'); 
INSERT INTO order_tb VALUES(104,11,100,'2022-09-02 11:00:00'); 
INSERT INTO order_tb VALUES(105,12,150,'2022-09-02 12:00:00'); 
INSERT INTO order_tb VALUES(106,12,1200,'2022-09-02 13:00:00'); 
INSERT INTO order_tb VALUES(107,11,60,'2022-09-03 09:00:00'); 
INSERT INTO order_tb VALUES(108,13,380,'2022-09-03 09:30:00'); 

drop table if exists  `visit_tb` ; 
CREATE TABLE `visit_tb` (
`info_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`visit_time` datetime NOT NULL,
`leave_time` datetime NOT NULL,
PRIMARY KEY (`info_id`));
INSERT INTO visit_tb VALUES(0911,10,'2022-09-01 08:00:00','2022-09-01 09:02:00'); 
INSERT INTO visit_tb VALUES(0912,11,'2022-09-01 08:30:00','2022-09-01 09:10:00'); 
INSERT INTO visit_tb VALUES(0913,12,'2022-09-01 09:50:00','2022-09-01 10:12:00'); 
INSERT INTO visit_tb VALUES(0914,13,'2022-09-01 11:40:00','2022-09-01 12:22:00'); 
INSERT INTO visit_tb VALUES(0921,11,'2022-09-02 10:30:00','2022-09-02 11:05:00'); 
INSERT INTO visit_tb VALUES(0922,11,'2022-09-02 12:00:00','2022-09-02 12:02:00'); 
INSERT INTO visit_tb VALUES(0923,12,'2022-09-02 11:40:00','2022-09-02 13:15:00'); 
INSERT INTO visit_tb VALUES(0924,13,'2022-09-02 09:00:00','2022-09-02 09:02:00'); 
INSERT INTO visit_tb VALUES(0925,14,'2022-09-02 10:00:00','2022-09-02 10:40:00'); 
INSERT INTO visit_tb VALUES(0931,10,'2022-09-03 09:00:00','2022-09-03 09:22:00'); 
INSERT INTO visit_tb VALUES(0932,11,'2022-09-03 08:30:00','2022-09-03 09:10:00'); 
INSERT INTO visit_tb VALUES(0933,13,'2022-09-03 09:00:00','2022-09-03 09:32:00'); 

输出

date|cr
2022-09-01|75.0%
2022-09-02|50.0%
2022-09-03|66.7%
头像 牛客题解官
发表于 2025-02-24 11:00:52
精华题解 这道题目要求我们计算商城每天的用户转化率,我们要做的事情如下: 1. 确定总体问题 我们需要计算每天的用户转化率,即在某一天访问商城的用户中有多少比例下了订单。转化率以百分数形式输出,并保留一位小数。转化率的计算公式是:下订单的用户数 / 访问用户数。 2. 分析关键问题 计算每日访问用户数:从v 展开全文
头像 牛客466955756号
发表于 2025-02-18 11:18:50
select date(visit_time) as date, concat(round(count(distinct ot.user_id) / 展开全文
头像 青柠提拉米苏
发表于 2024-12-26 20:09:21
select df as date, concat(round(dan/fang*100,1),'%') cr from (select date_format(order_time,'%Y-%m-%d') df, count(distinct user_i 展开全文
头像 路静
发表于 2025-07-09 17:44:07
select date, concat(round(id_cnt/vi_cnt*100,1),'%') cr from ( select date(order_time) date, count(distinct m.user_id) id_cnt, count(distin 展开全文
头像 牛客938846508号
发表于 2025-04-26 14:57:44
#order_tb: order_id #visit_tb: user_id+visit_time #需求 访问-下单 -- 这种登录问题,一定要去重,某个用户一天内多次访问丨登录的情况 select date(visit_time) as date ,concat(round(count(d 展开全文
头像 刷牛客的coder很爱吃香菜
发表于 2025-02-12 13:56:51
with -- 每天的访问人数(存在一个用户在一天内多次访问的情况,需去重)) t1 as ( select date(visit_time) v_time, count(distinct user_id) v_num 展开全文
头像 在思考的六边形战士很想去旅行
发表于 2025-08-01 14:33:07
select date(ot.order_time) as date, concat(round(count(distinct ot.user_id)*100/count(distinct vt.user_id),1),'%') as cr from visit_tb vt 展开全文
头像 数据分析仔
发表于 2025-02-18 11:27:52
select ordertime as date, concat(round(orderid / visitid*100, 1) ,'%') as cr from ( select count(distinct user_id) as 展开全文
头像 风流倜傥三金老师
发表于 2025-10-31 11:44:05
WITH t1 AS ( -- 计算每日下单人数 SELECT COUNT(DISTINCT user_id) AS dds, DATE(order_time) AS ddt FROM order_tb GROUP BY DATE(or 展开全文
头像 Aki5
发表于 2025-08-18 09:35:30
-- 1. 处理订单表:获取每个用户每日首次下单日期(去重,同一用户单日仅计1次) WITH t1 AS ( SELECT user_id, MIN(DATE(order_time)) AS order_time_1 -- 取用户当日首次下单日期 展开全文
头像 轩辕十四14
发表于 2025-07-16 09:30:19
with ox as( select DATE_FORMAT(o.order_time, '%Y-%m-%d') AS date, count(distinct o.user_id) ou from order_tb o group by DAT 展开全文