-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy path20-comments-histogram.sql
More file actions
39 lines (31 loc) · 963 Bytes
/
20-comments-histogram.sql
File metadata and controls
39 lines (31 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
WITH CTE as (
SELECT u.id, COUNT(c.user_id) as comment_count
FROM users u LEFT JOIN comments c
ON u.id = c.user_id
and c.created_at BETWEEN '2020-01-01' AND '2020-01-31'
GROUP BY u.id
)
SELECT comment_count, COUNT(*) as frequency
FROM CTE
GROUP BY comment_count
-- my initial attempt
-- should have just used LEFT JOIN
WITH CTE as (
SELECT *,
CASE
WHEN created_at BETWEEN '2020-01-01' and '2020-01-31'
THEN 'yes'
ELSE 'no'
END as to_consider
FROM comments
)
,cte_2 as (
SELECT u.id, COUNT(body) FILTER(WHERE to_consider='yes') as yes_count
FROM CTE c JOIN users u ON c.user_id=u.id
GROUP BY u.id
)
SELECT yes_count, COUNT(*) as frequency
FROM cte_2
GROUP BY yes_count
ORDER BY yes_count
-- NOTE: query doesn't work where the month condition is in the WHERE clause. have to put it in the JOIN condition itself.