Skip to main content

LeetCode 1132. Reported Posts II SQL Solution

Problem

LeetCode SQL Problem

  1. Reported Posts II

Actions table

user_idpost_idaction_dateactionextra
112019-07-01viewnull
112019-07-01likenull
112019-07-01sharenull
222019-07-04viewnull
222019-07-04reportspam
342019-07-04viewnull
342019-07-04reportspam
432019-07-02viewnull
432019-07-02reportspam
522019-07-03viewnull
522019-07-03reportracism
552019-07-03viewnull
552019-07-03reportracism

Removals table

post_idremove_date
22019-07-20
32019-07-18

Solution

Reported Posts II
WITH PostRemovalRate
AS (
-- Perform a LEFT JOIN so we can calculate daily removal rate of reported spams
SELECT A.action_date
,count(R.post_id) / cast(count(A.post_id) AS FLOAT) AS removal_rate
FROM Actions AS A
LEFT JOIN Removals AS R ON A.post_id = R.post_id
WHERE A.action = 'report'
AND A.extra = 'spam'
GROUP BY A.action_date
)
-- Find the average for daily percentage of posts that got removed after being reported as spam
-- Round to 2 decimal places
SELECT round(avg(removal_rate), 2) * 100 AS average_daily_percent
FROM PostRemovalRate

Query Output

average_daily_percent
75