-
Notifications
You must be signed in to change notification settings - Fork 3
/
hashtag_statistics.sql
109 lines (108 loc) · 2.67 KB
/
hashtag_statistics.sql
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
DROP MATERIALIZED VIEW IF EXISTS hashtag_statistics;
CREATE MATERIALIZED VIEW hashtag_statistics AS
WITH general AS (
SELECT
hashtag_id,
max(coalesce(closed_at, created_at)) last_edit,
count(*) changeset_count,
sum(coalesce(total_edits, 0)) edit_count,
max(updated_at) updated_at
FROM changesets
JOIN changesets_hashtags ON changesets.id = changesets_hashtags.changeset_id
GROUP BY hashtag_id
),
processed_changesets AS (
SELECT
id,
user_id,
hashtag_id,
measurements,
counts,
total_edits
FROM changesets
JOIN changesets_hashtags ON changesets.id = changesets_hashtags.changeset_id
),
user_counts AS (
SELECT
RANK() OVER (PARTITION BY hashtag_id ORDER BY sum(coalesce(total_edits, 0)) DESC) AS rank,
hashtag_id,
user_id,
count(*) changesets,
sum(coalesce(total_edits, 0)) edit_count
FROM processed_changesets
GROUP BY hashtag_id, user_id
),
users AS (
SELECT
hashtag_id,
jsonb_object_agg(user_id, changesets) user_changesets,
jsonb_object_agg(user_id, edit_count) user_edits
FROM user_counts
WHERE rank <= 10
GROUP BY hashtag_id
),
measurements AS (
SELECT
id,
hashtag_id,
key,
value
FROM processed_changesets
CROSS JOIN LATERAL jsonb_each(measurements)
),
aggregated_measurements_kv AS (
SELECT
hashtag_id,
key,
sum((value->>0)::numeric) AS value
FROM measurements
GROUP BY hashtag_id, key
),
aggregated_measurements AS (
SELECT
hashtag_id,
jsonb_object_agg(key, value) measurements
FROM aggregated_measurements_kv
GROUP BY hashtag_id
),
counts AS (
SELECT
id,
hashtag_id,
key,
value
FROM processed_changesets
CROSS JOIN LATERAL jsonb_each(counts)
),
aggregated_counts_kv AS (
SELECT
hashtag_id,
key,
sum((value->>0)::numeric) AS value
FROM counts
GROUP BY hashtag_id, key
),
aggregated_counts AS (
SELECT
hashtag_id,
jsonb_object_agg(key, value) counts
FROM aggregated_counts_kv
GROUP BY hashtag_id
)
SELECT
hashtags.hashtag tag,
general.hashtag_id,
measurements,
counts,
general.changeset_count,
general.edit_count,
general.last_edit,
general.updated_at,
user_changesets,
user_edits
FROM general
JOIN hashtags ON hashtag_id = hashtags.id
LEFT OUTER JOIN users USING (hashtag_id)
LEFT OUTER JOIN aggregated_measurements USING (hashtag_id)
LEFT OUTER JOIN aggregated_counts USING (hashtag_id);
CREATE UNIQUE INDEX IF NOT EXISTS hashtag_statistics_hashtag_id ON hashtag_statistics(hashtag_id);