-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalytics_db_wrangling.sql
54 lines (50 loc) · 1.61 KB
/
analytics_db_wrangling.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
SELECT *
FROM raw
WHERE timestamp > '2019-11-28 10:30'
AND environment = 'development'
CREATE TABLE lessraw AS
(
SELECT environment,
timestamp,
event ->> 'clientId' AS client_id,
event ->> 'userId' AS user_id,
event ->> 'tabId' AS tab_id,
event ->> 'url' AS url,
event ->> 'referrer' AS referrer
FROM raw
WHERE event_type = 'pageLoadFinished'
AND environment = 'lesswrong.com'
AND timestamp BETWEEN '2019-11-26' AND '2019-11-27'
);
SELECT *
FROM raw
WHERE timestamp > '2019-11-28 10:30'
AND environment = 'development'
WITH filtered_raw AS
(SELECT * FROM raw WHERE environment='development' AND timestamp > '2019-11-28 11:30')
SELECT * FROM
(
SELECT environment,
timestamp,
event ->> 'clientId' AS client_id,
event ->> 'userId' AS user_id,
event ->> 'tabId' AS tab_id,
event ->> 'listContext' AS list_context,
jsonb_array_elements_text(event -> 'postIds') AS post_id
FROM filtered_raw
WHERE event_type = 'postListMounted'
UNION
SELECT environment,
timestamp,
event ->> 'clientId' AS client_id,
event ->> 'userId' AS user_id,
event ->> 'tabId' AS tab_id,
event ->> 'listContext' AS list_context,
event ->> 'postId' AS post_id
FROM filtered_raw
WHERE event_type = 'postItemMounted'
) post_items_mounted
LEFT JOIN
(SELECT _id , title, username AS author, base_score FROM posts) posts
ON posts._id = post_items_mounted.post_id
ORDER BY user_id DESC, tab_id, list_context, timestamp ASC