-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStrataScratch 10284 Popularity Percentage.sql
More file actions
59 lines (50 loc) · 1.5 KB
/
StrataScratch 10284 Popularity Percentage.sql
File metadata and controls
59 lines (50 loc) · 1.5 KB
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
set search_path to data_sci
CREATE TABLE facebook_friends (
user1 int,
user2 int
)
-- Find the popularity percentage for each user on Meta/Facebook. The popularity percentage
-- is defined as the total number of friends the user has divided by the total number of users
-- on the platform, then converted into a percentage by multiplying by 100.
-- Output each user along with their popularity percentage. Order records in ascending order
-- by user id. The 'user1' and 'user2' column are pairs of friends.
with distinct_users as (
select f1.user1 as fb_user
from facebook_friends f1
union
select f2.user2 as fb_user
from facebook_friends f2
),
pairs as (
select array[user1,user2] as fb_pair
from facebook_friends
),
friend_count as (
select du.fb_user,
sum(case when du.fb_user = any(fb_pair) then 1 else 0 end ) as friend_cnt
from distinct_users du
cross join pairs
group by du.fb_user
)
select fc.fb_user,
round(((fc.friend_cnt::real/
(select count(fb_user) from distinct_users))*100)::numeric,2) as popularity_percentage
from friend_count fc
--------------------------------------
-- The instructor solution is better
-- Create the union of pairs and then count and group
WITH users_union AS
(SELECT user1,
user2
FROM facebook_friends
UNION
SELECT user2 AS user1,
user1 AS user2
FROM facebook_friends)
SELECT user1,
count(*)::float /
(SELECT count(DISTINCT user1) FROM users_union)
*100 AS popularity_percent
FROM users_union
GROUP BY 1
ORDER BY 1