首页 > 其他分享 >用户在某天刷题后第二天还会再来刷题的平均概率

用户在某天刷题后第二天还会再来刷题的平均概率

时间:2022-08-18 16:00:44浏览次数:56  
标签:practice question 用户 detail 2021 date 天刷题 ID 刷题

======题目=============================
用户在某天刷题后第二天还会再来刷题的平均概率。请你取出相应数据

======oracle数据库=================
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (1, 2138, 111, 'wrong', to_date('03-05-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (2, 3214, 112, 'wrong', to_date('09-05-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (3, 3214, 113, 'wrong', to_date('15-06-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (4, 6543, 111, 'right', to_date('13-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (5, 2315, 115, 'right', to_date('13-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (6, 2315, 116, 'right', to_date('14-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (7, 2315, 117, 'wrong', to_date('15-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (8, 3214, 112, 'wrong', to_date('09-05-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (9, 3214, 113, 'wrong', to_date('15-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (10, 6543, 111, 'right', to_date('13-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (11, 2315, 115, 'right', to_date('13-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (12, 2315, 116, 'right', to_date('14-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (13, 2315, 117, 'wrong', to_date('15-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (14, 3214, 112, 'wrong', to_date('16-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (15, 3214, 113, 'wrong', to_date('18-08-2021', 'dd-mm-yyyy'));
insert into question_practice_detail (ID, DEVICE_ID, QUESTION_ID, RESULT, DATES) values (16, 6543, 111, 'right', to_date('13-08-2021', 'dd-mm-yyyy'));

======oracle数据库sql=================

select count(Main.device_id) , sum( case when Main.flag=1 then 1 else 0 end ) , sum( case when Main.flag=1 then 1 else 0 end ) / count(Main.device_id) from (
select
device_id,
dates,
lead(dates,1,null) over (partition by device_id order by dates) as date2 , lead(dates,1,null) over (partition by device_id order by dates)- dates flag
from (
select distinct device_id, dates
from question_practice_detail
)
)Main

 

======mysql数据库=================
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL,
`date` date NOT NULL
);

INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');


======mysql数据库sql=================
限定条件:第二天再来。
解法1:表里的数据可以看作是全部第一天来刷题了的,那么我们需要构造出第二天来了的字段,因此可以考虑用left join把第二天来了的拼起来,限定第二天来了的可以用date_add(date1, interval 1 day)=date2筛选,并用device_id限定是同一个用户。
解法2:用lead函数将同一用户连续两天的记录拼接起来。先按用户分组partition by device_id,再按日期升序排序order by date,再两两拼接(最后一个默认和null拼接),即lead(date) over (partition by device_id order by date)
平均概率:
解法1:可以count(date1)得到左表全部的date记录数作为分母,count(date2)得到右表关联上了的date记录数作为分子,相除即可得到平均概率
解法2:检查date2和date1的日期差是不是为1,是则为1(次日留存了),否则为0(次日未留存),取avg即可得平均概率。
附:lead用法,date_add用法,datediff用法,date函数

 

SELECT
COUNT(q2.device_id) / COUNT(q1.device_id) AS avg_ret
FROM
(SELECT DISTINCT device_id, date FROM question_practice_detail)as q1
LEFT JOIN
(SELECT DISTINCT device_id, date FROM question_practice_detail) AS q2
ON q1.device_id = q2.device_id AND q2.date = DATE_ADD(q1.date, interval 1 day)

标签:practice,question,用户,detail,2021,date,天刷题,ID,刷题
From: https://www.cnblogs.com/CloudHaiYun/p/16599001.html

相关文章

  • 手机网页限制用户缩放代码 (2014-03-25 18:16:52)
    网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放。    width-viewport的宽度height-viewport的高度  initi......
  • 6、Python语法入门之与用户交互、运算符
    6、Python语法入门之与用户交互、运算符  目录一程序与用户交互1.1什么是与用户交互?1.2为什么要与用户交互?1.3如何与用户交互1.3.1输入inpu......
  • 工具模板 | 用APOEM方法消除对用户行为的偏见
    如何降低人们的偏见,观察并记录真实的用户行为?首先需要大家每个人从多个维度去观察,只对事实进行记录,互相不批评、不评论、不议论。在这篇文章中,我们来介绍一个具体的消除......
  • 如何查看navicat已连接用户的密码
    1:从注册表中查看加密后的密码。1.1:windows键+r,输入regedit 1.2:在注册表中找到\HKEY_CURRENT_USER\SOFTWARE\PremiumSoft\navicat\servers 并找到UserName......
  • mongo容器忘记了root用户账号密码
    需要执行root账号的操作时错:Error:notauthorizedonadmintoexecutecommand轻松三步解决1.关闭认证,需要去该Mongo服务的部署信息去掉"--auth",然后再去部署2.直接用mo......
  • 用户身份切换
    用户身份切换su概述su是最简单的身份切换指令,它可以进行任何身份的切换使用su需要知道新切换的用户的密码(通常是需要root的密码)使用root切换为任何用户时,并不需......
  • 用户和用户组文件
    用户和用户组文件/etc/passwd完成用户管理的工作有许多种方法,但是每一种方法实际上都是对有关的系统文件进行修改在Linux系统中,所创建的用户帐号和其相关信息(密......
  • 用户组管理
    用户组管理概述简介每个用户都有一个用户组,系统可以对一个用户组中的所有用户进行集中管理(开发、测试、运维、root)。不同Linux系统对用户组的规定有所不同,如Linux下......
  • 【.Net力扣刷题】第1302题:层数最深叶子节点的和
    题目描述来源:力扣(LeetCode)链接:https://leetcode.cn/problems/deepest-leaves-sum/给你一棵二叉树的根节点root,请你返回层数最深的叶子节点的和。题目分析本题需......
  • Java用户交互Scanner
    Scanner1、Scanner对象Scanner是Java给我们提供的一个工具类,我们可以通过Scanner类来获取用户的输入。下面是创建Scanner对象的基本语法: Scanners=newScanner......