-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_query.sql
36 lines (32 loc) · 1.48 KB
/
a_query.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
-- Initializing the Base-case: Counting Followers
WITH RECURSIVE FollowerCount (root_person_id, follower_id, hops)
AS (
SELECT
p.id, -- root_person_id placeholder
p.id, -- follower_id placeholder, updated later with last follower
1 AS hops -- hops set to 1, indicating the initial starting point
FROM person AS p
INNER JOIN follow AS f ON (p.id = f.followed_person_id)
WHERE p.year_of_birth = 1990 AND f.follows_since BETWEEN '2015-06-01' AND '2016-06-01'
UNION ALL
-- Recursive step: Traverse the follower relationships up to 5 hops
SELECT
fc.root_person_id, -- each recursion step selects root_person_id
f.person_id, -- person_id from the previous step in the follow table, up to 5 hops
fc.hops + 1 -- increment hops by 1 in each recursive step
FROM FollowerCount AS fc
INNER JOIN follow AS f ON (fc.follower_id = f.followed_person_id)
WHERE f.follows_since BETWEEN '2015-06-01' AND '2016-06-01'
AND fc.hops < 5
)
-- Final query: Format and order the results
SELECT
fc.root_person_id,
p.firstname,
p.lastname,
fc.hops,
COUNT(fc.follower_id) AS follower_count
FROM FollowerCount AS fc
INNER JOIN person AS p ON (fc.root_person_id = p.id)
GROUP BY fc.root_person_id, p.firstname, p.lastname, fc.hops
ORDER BY fc.root_person_id, fc.hops;