MySQL

[StrataScratch] Popularity Percentage [MySQL]

치킨먹고싶어요 2022. 6. 27. 11:18

https://platform.stratascratch.com/coding/10284-popularity-percentage?code_type=3 

 

StrataScratch

 

platform.stratascratch.com

코드
WITH friends AS (
    SELECT user1 p1, user2 p2
    FROM facebook_friends
    UNION
    Select user2 p1, user1 p2
    FROM facebook_friends
)
 
SELECT p1, COUNT(*/ (
    SELECT COUNT(*)
    FROM facebook_friends
* 100
FROM friends
GROUP BY p1
ORDER BY p1;
cs

 

풀이

1. 친구 관계가 중복되지 않으므로 facebook_friends의 전체 개수는 총 사람의 수입니다.

2. p1과 p2에서 나오는 자기 자신의 개수의 합은 친구의 합입니다.