返回介绍

solution / 1100-1199 / 1132.Reported Posts II / README_EN

发布于 2024-06-17 01:03:23 字数 3731 浏览 0 评论 0 收藏 0

1132. Reported Posts II

中文文档

Description

Table: Actions

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| user_id     | int   |
| post_id     | int   |
| action_date   | date  | 
| action    | enum  |
| extra     | varchar |
+---------------+---------+
This table may have duplicate rows.
The action column is an ENUM (category) type of ('view', 'like', 'reaction', 'comment', 'report', 'share').
The extra column has optional information about the action, such as a reason for the report or a type of reaction.

 

Table: Removals

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| post_id     | int   |
| remove_date   | date  | 
+---------------+---------+
post_id is the primary key (column with unique values) of this table.
Each row in this table indicates that some post was removed due to being reported or as a result of an admin review.

 

Write a solution to find the average daily percentage of posts that got removed after being reported as spam, rounded to 2 decimal places.

The result format is in the following example.

 

Example 1:

Input: 
Actions table:
+---------+---------+-------------+--------+--------+
| user_id | post_id | action_date | action | extra  |
+---------+---------+-------------+--------+--------+
| 1     | 1     | 2019-07-01  | view   | null   |
| 1     | 1     | 2019-07-01  | like   | null   |
| 1     | 1     | 2019-07-01  | share  | null   |
| 2     | 2     | 2019-07-04  | view   | null   |
| 2     | 2     | 2019-07-04  | report | spam   |
| 3     | 4     | 2019-07-04  | view   | null   |
| 3     | 4     | 2019-07-04  | report | spam   |
| 4     | 3     | 2019-07-02  | view   | null   |
| 4     | 3     | 2019-07-02  | report | spam   |
| 5     | 2     | 2019-07-03  | view   | null   |
| 5     | 2     | 2019-07-03  | report | racism |
| 5     | 5     | 2019-07-03  | view   | null   |
| 5     | 5     | 2019-07-03  | report | racism |
+---------+---------+-------------+--------+--------+
Removals table:
+---------+-------------+
| post_id | remove_date |
+---------+-------------+
| 2     | 2019-07-20  |
| 3     | 2019-07-18  |
+---------+-------------+
Output: 
+-----------------------+
| average_daily_percent |
+-----------------------+
| 75.00         |
+-----------------------+
Explanation: 
The percentage for 2019-07-04 is 50% because only one post of two spam reported posts were removed.
The percentage for 2019-07-02 is 100% because one post was reported as spam and it was removed.
The other days had no spam reports so the average is (50 + 100) / 2 = 75%
Note that the output is only one number and that we do not care about the remove dates.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  T AS (
    SELECT
      COUNT(DISTINCT t2.post_id) / COUNT(DISTINCT t1.post_id) * 100 AS percent
    FROM
      Actions AS t1
      LEFT JOIN Removals AS t2 ON t1.post_id = t2.post_id
    WHERE extra = 'spam'
    GROUP BY action_date
  )
SELECT ROUND(AVG(percent), 2) AS average_daily_percent
FROM T;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文