Skip to main content

LeetCode 1149. Article Views II SQL Solution

Problem

LeetCode SQL Problem

  1. Article Views II

Views table

article_idauthor_idviewer_idview_date
1352019-08-01
3452019-08-01
1362019-08-02
2772019-08-01
2762019-08-02
4712019-07-22
3442019-07-21
3442019-07-21

Solution

Article Views II
WITH ArticleViewsByDateViewerId
AS (
-- For each date, find out how many distinct articles each viewer reads
SELECT view_date
,viewer_id
,count(DISTINCT article_id) AS article_views
FROM Views
GROUP BY view_date
,viewer_id
)
-- Return viewers who viewed more than one article on the same date
SELECT DISTINCT (viewer_id) AS id
FROM ArticleViewsByDateViewerId
WHERE article_views > 1
-- Sort viewer_id in ascending order
ORDER BY viewer_id

Query Output

id
5
6