首页 > 数据库 >力扣1132(MySQL)-报告的记录Ⅱ(中等)

力扣1132(MySQL)-报告的记录Ⅱ(中等)

时间:2023-04-13 10:14:59浏览次数:41  
标签:07 1132 力扣 2019 MySQL date post id

题目:

编写一段 SQL 来查找:在被报告为垃圾广告的帖子中,被移除的帖子的每日平均占比,四舍五入到小数点后 2 位。

Actions 表:

 Removals 表:

Result 表:

2019-07-04 的垃圾广告移除率是 50%,因为有两张帖子被报告为垃圾广告,但只有一个得到移除。
2019-07-02 的垃圾广告移除率是 100%,因为有一张帖子被举报为垃圾广告并得到移除。
其余几天没有收到垃圾广告的举报,因此平均值为:(50 + 100) / 2 = 75%
注意,输出仅需要一个平均值即可,我们并不关注移除操作的日期。

建表语句:

 1 Create table If Not Exists actions_1132 (
 2     user_id int, 
 3         post_id int,
 4         action_date date, 
 5         action enum('view', 'like', 'reaction', 'comment', 'report', 'share'),
 6         extra varchar(6)
 7 );
 8 Create table If Not Exists removals_1132 (
 9         post_id int,
10         remove_date date
11     );
12 Truncate table actions_1132;
13 insert into actions_1132 (user_id, post_id, action_date, action, extra) values 
14 ('1', '1', '2019-07-01','view',null),
15 ('1', '1', '2019-07-01','like',null),
16 ('1', '1', '2019-07-01','share',null),
17 ('2', '2', '2019-07-04','view',null),
18 ('2', '2', '2019-07-04','report','spam'),
19 ('3', '4', '2019-07-04','view',null),
20 ('3', '4', '2019-07-04','report','spam'),
21 ('4', '3', '2019-07-02','view',null),
22 ('4', '3', '2019-07-02','report','spam'),
23 ('5', '2', '2019-07-03','view',null),
24 ('5', '2', '2019-07-03','report','racism'),
25 ('5', '5', '2019-07-03','view',null),
26 ('5', '5', '2019-07-03','report','racism');
27 Truncate table removals_1132;
28 insert into removals_1132 (post_id, remove_date) values ('2', '2019-07-20'),( '3', '2019-07-18');

解题思路:

①先筛选出actions表中被报道为垃圾信息即extra = 'spam'的数据;

1 select post_id, action_date
2 from actions_1132 
3 where extra = 'spam'

②将第一步查询出来的表通过post_id与 removals左连接;

1 select *
2 from (
3     select post_id, action_date
4     from actions_1132 
5     where extra = 'spam'
6   ) as a
7 left join removals_1132 b
8 on a.post_id = b.post_id

 ③然后再根据第二步查询出的临时表,以action_date为分组,统计出删除比率;

1   select action_date,count(distinct b.post_id) / count(distinct a.post_id) as rate
2   from (
3     select post_id, action_date
4     from actions_1132 
5     where extra = 'spam'
6   ) as a
7   left join removals_1132 b
8   on a.post_id = b.post_id
9   group by action_date

 ④最后根据上一步的临时表,计算出被移除的帖子的每日平均占比;

 1 select round(sum(rate)/ count(*)*100, 2) as average_daily_percent
 2 from (
 3   select action_date,count(distinct b.post_id) / count(distinct a.post_id) as rate
 4   from (
 5     select post_id, action_date
 6     from actions_1132 
 7     where extra = 'spam'
 8   ) as a
 9   left join removals_1132 b
10   on a.post_id = b.post_id
11   group by action_date
12 ) as temp

标签:07,1132,力扣,2019,MySQL,date,post,id
From: https://www.cnblogs.com/liu-myu/p/17312342.html

相关文章

  • Linux将MySQL数据库目录挂载至新数据盘
    https://www.bbsmax.com/A/QW5YRE1Mdm/具体操作 Mysql如何挂盘?使用linux下的软连接方法,具体操作如下:(以迁移到/home/mysql/目录为例)先停止mysql:/etc/init.d/mysqldstop移动数据:mv/var/lib/mysql/*/home/mysql/创建软连接:ln-s/home/mysql//var/lib/mysql/启动......
  • mysql主从1062主键冲突跳过错误
    1062错误——主键冲突,出现这种情况就是从库出现插入操作,主库又插入相同的数据,iothread没问题,sqlthread出错处理此种错误一般有两种思路:1、直接跳过错误执行语句2、找到错误执行语句,修复主库2数据https://www.cndba.cn/leo1990/article/2957https://www.cndba.cn/leo1990/articl......
  • qrtz表初始化脚本_mysql
    DROPTABLEIFEXISTSqrtz_blob_triggers;DROPTABLEIFEXISTSqrtz_calendars;DROPTABLEIFEXISTSqrtz_cron_triggers;DROPTABLEIFEXISTSqrtz_fired_triggers;DROPTABLEIFEXISTSqrtz_locks;DROPTABLEIFEXISTSqrtz_paused_trigger_grps;DROPTABLEIF......
  • Mysql 入门之过滤数据(二)
    关键字:and、or、in、not知识点1、and并列关系,可对多个列进行过滤2、or或关系,满足条件之一即可3、in指定范围过滤4、not否定关系,取反思考1、and和or若同时存在,哪个优先级最高?2、in使用的优势?3、not存在的意义是什么?案列数据表如下:SELECT*FROMproducts......
  • Mysql四种事务隔离级别
    先了解一下事务的四大特性:ACID原子性(Atomicity)原子性就是不可拆分的特性,要么全部成功然后提交(commit),要么全部失败然后回滚(rollback)。MySQL通过RedoLog重做日志实现了原子性,在将执行SQL语句时,会先写入redologbuffer,再执行SQL语句,若SQL语句执行出错就会根据redologbuffer中的......
  • Mysql 入门之过滤数据(一)
    关键字:where、between、null知识点1、where关键字在Mysql中用于获取指定搜素条件的数据,这些条件也称之为过滤条件2、between为where子句的操作符,诸如=、<、>等符号功能类似3、关于null可以初步理解为它是Mysql中不确定的“值”,从根本上讲它都不是个“值”下面给出一些案......
  • cnetos8上RPM安装mysql8后,修改初始密码提示Your password does not satisfy the curre
    我在修改mysql8初始密码是遇到了Yourpassworddoesnotsatisfythecurrentpolicyrequirements,如果您的mysql版本5.x可能不太合适。下图是我遇到的问题: 意思是,密码不符合密码验证要求。但是mysql8的初始密码连验证要求都查不到,不过一般密码强度是不过我们可以密码设置为......
  • 力扣---剑指 Offer 39. 数组中出现次数超过一半的数字
    数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。你可以假设数组是非空的,并且给定的数组总是存在多数元素。示例 1:输入:[1,2,3,2,2,2,5,4,2]输出:2 限制:1<=数组长度<=50000注意:本题与主站169题相同:https://leetcode-cn.com/problems/majority-el......
  • mysql性能优化二
    索引:高校检索数据的数据结构索引能干吗呢‘?索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构。索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要。索引优化应该是对查询性能优化最有效的手段了。索引能够轻易将查询性能提......
  • centos安装MySQL8记录
    1.卸载mariadb因centos7默认安装了mariadb,会造成依赖冲突,按下列方式进行卸载:rpm-qa|grepmariadb#如果出现mariadb-libs-5.5.68-1.el7.x86_64#执行rpm-emariadb-libs--nodeps2.下载  注意权限:(chmod-R777/var/lib/mysql)(chmod-R777/var......