SQL185. 牛客直播转换率
描述
course_id | course_name | course_datetime |
1 | Python | 2021-12-1 19:00-21:00 |
2 | SQL | 2021-12-2 19:00-21:00 |
3 | R | 2021-12-3 19:00-21:00 |
user_id | if_vw | if_fav | if_sign | course_id |
100 | 1 | 1 | 1 | 1 |
100 | 1 | 1 | 1 | 2 |
100 | 1 | 1 | 1 | 3 |
101 | 1 | 1 | 1 | 1 |
101 | 1 | 1 | 1 | 2 |
101 | 1 | 0 | 0 | 3 |
102 | 1 | 1 | 1 | 1 |
102 | 1 | 1 | 1 | 2 |
102 | 1 | 1 | 1 | 3 |
103 | 1 | 1 | 0 | 1 |
103 | 1 | 0 | 0 | 2 |
103 | 1 | 0 | 0 | 3 |
104 | 1 | 1 | 1 | 1 |
104 | 1 | 1 | 1 | 2 |
104 | 1 | 1 | 0 | 3 |
105 | 1 | 0 | 0 | 1 |
106 | 1 | 0 | 0 | 1 |
107 | 1 | 0 | 0 | 1 |
107 | 1 | 1 | 1 | 2 |
108 | 1 | 1 | 1 | 3 |
course_id | course_name | sign_rate(%) |
1 | Python | 50.00 |
2 | SQL | 83.33 |
3 | R | 50.00 |
示例1
输入:
drop table if exists course_tb; CREATE TABLE course_tb( course_id int(10) NOT NULL, course_name char(10) NOT NULL, course_datetime char(30) NOT NULL); INSERT INTO course_tb VALUES(1, 'Python', '2021-12-1 19:00-21:00'); INSERT INTO course_tb VALUES(2, 'SQL', '2021-12-2 19:00-21:00'); INSERT INTO course_tb VALUES(3, 'R', '2021-12-3 19:00-21:00'); drop table if exists behavior_tb; CREATE TABLE behavior_tb( user_id int(10) NOT NULL, if_vw int(10) NOT NULL, if_fav int(10) NOT NULL, if_sign int(10) NOT NULL, course_id int(10) NOT NULL); INSERT INTO behavior_tb VALUES(100, 1, 1, 1, 1); INSERT INTO behavior_tb VALUES(100, 1, 1, 1, 2); INSERT INTO behavior_tb VALUES(100, 1, 1, 1, 3); INSERT INTO behavior_tb VALUES(101, 1, 1, 1, 1); INSERT INTO behavior_tb VALUES(101, 1, 1, 1, 2); INSERT INTO behavior_tb VALUES(101, 1, 0, 0, 3); INSERT INTO behavior_tb VALUES(102, 1, 1, 1, 1); INSERT INTO behavior_tb VALUES(102, 1, 1, 1, 2); INSERT INTO behavior_tb VALUES(102, 1, 1, 1, 3); INSERT INTO behavior_tb VALUES(103, 1, 1, 0, 1); INSERT INTO behavior_tb VALUES(103, 1, 0, 0, 2); INSERT INTO behavior_tb VALUES(103, 1, 0, 0, 3); INSERT INTO behavior_tb VALUES(104, 1, 1, 1, 1); INSERT INTO behavior_tb VALUES(104, 1, 1, 1, 2); INSERT INTO behavior_tb VALUES(104, 1, 1, 0, 3); INSERT INTO behavior_tb VALUES(105, 1, 0, 0, 1); INSERT INTO behavior_tb VALUES(106, 1, 0, 0, 1); INSERT INTO behavior_tb VALUES(107, 1, 0, 0, 1); INSERT INTO behavior_tb VALUES(107, 1, 1, 1, 2); INSERT INTO behavior_tb VALUES(108, 1, 1, 1, 3);
输出:
1|Python|50.00 2|SQL|83.33 3|R|50.00
Mysql 解法, 执行用时: 36ms, 内存消耗: 6636KB, 提交时间: 2021-12-14
select a.course_id, b.course_name, round(sum(if_sign)/sum(if_vw)*100,2) `sign_rate(%)` from behavior_tb a left join course_tb b on a.course_id = b.course_id group by a.course_id , b.course_name order by a.course_id
Mysql 解法, 执行用时: 37ms, 内存消耗: 6392KB, 提交时间: 2022-01-26
select course_id, course_name, round((sum(if_sign)/sum(if_vw))*100,2) 'sign_rate(%)' from course_tb ct join behavior_tb bt using(course_id) group by 1
Mysql 解法, 执行用时: 37ms, 内存消耗: 6392KB, 提交时间: 2022-01-25
select c.course_id, c.course_name, round(sum(if_sign)/sum(if_vw)*100 ,2)as sign_rate from course_tb c join behavior_tb b on c.course_id=b.course_id group by b.course_id,course_name # 注意此处的course_name也需要加入group by, # 不然只对course_id进行group by不能带出其name order by c.course_id
Mysql 解法, 执行用时: 37ms, 内存消耗: 6396KB, 提交时间: 2021-12-18
select behavior_tb.course_id, course_tb.course_name, round(sum(if_sign)/sum(if_vw)*100,2)as 'sign_rate(%)' from course_tb right join behavior_tb on course_tb.course_id=behavior_tb.course_id group by behavior_tb.course_id,course_tb.course_name order by behavior_tb.course_id asc
Mysql 解法, 执行用时: 37ms, 内存消耗: 6396KB, 提交时间: 2021-12-07
SELECT course_id,course_name,round(sum(if_sign)/sum(if_vw)*100,2) r from behavior_tb a left join course_tb b using(course_id) group by course_name,course_id order by course_id