難度中等
SQL架構
動作表:
Actions
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| post_id | int |
| action_date | date |
| action | enum |
| extra | varchar |
+---------------+---------+
這張表沒有主鍵,并有可能存在重複的行。
action 列的類型是 ENUM,可能的值為 ('view', 'like', 'reaction', 'comment', 'report', 'share')。
extra 列擁有一些可選資訊,例如:報告理由(a reason for report)或反應類型(a type of reaction)等。
複制
移除表:
Removals
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| post_id | int |
| remove_date | date |
+---------------+---------+
這張表的主鍵是 post_id。
這張表的每一行表示一個被移除的文章,原因可能是由于被舉報或被管理者審查。
複制
編寫一段 SQL 來查找:在被報告為垃圾廣告的文章中,被移除的文章的每日平均占比,四舍五入到小數點後 2 位。
查詢結果的格式如下:
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 |
+---------+-------------+
Result table:
+-----------------------+
| average_daily_percent |
+-----------------------+
| 75.00 |
+-----------------------+
2019-07-04 的垃圾廣告移除率是 50%,因為有兩張文章被報告為垃圾廣告,但隻有一個得到移除。
2019-07-02 的垃圾廣告移除率是 100%,因為有一張文章被舉報為垃圾廣告并得到移除。
其餘幾天沒有收到垃圾廣告的舉報,是以平均值為:(50 + 100) / 2 = 75%
注意,輸出僅需要一個平均值即可,我們并不關注移除操作的日期。
複制