|
| 1 | +# [2720. Popularity Percentage](https://leetcode.cn/problems/popularity-percentage) |
| 2 | + |
| 3 | +[English Version](/solution/2700-2799/2720.Popularity%20Percentage/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code>Friends</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++-------------+------+ |
| 13 | +| Column Name | Type | |
| 14 | ++-------------+------+ |
| 15 | +| user1 | int | |
| 16 | +| user2 | int | |
| 17 | ++-------------+------+ |
| 18 | +(user1, user2) is the primary key of this table. |
| 19 | +Each row contains information about friendship where user1 and user2 are friends. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p>Write an SQL query to 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, <strong>rounded to 2 decimal places</strong>.</p> |
| 23 | + |
| 24 | +<p>Return <em>the result table ordered by</em> <code>user1</code> <em>in <strong>ascending</strong> order.</em></p> |
| 25 | + |
| 26 | +<p>The query result format is in the following example.</p> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | +<p><strong class="example">Example 1:</strong></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> |
| 33 | +Friends table: |
| 34 | ++-------+-------+ |
| 35 | +| user1 | user2 | |
| 36 | ++-------+-------+ |
| 37 | +| 2 | 1 | |
| 38 | +| 1 | 3 | |
| 39 | +| 4 | 1 | |
| 40 | +| 1 | 5 | |
| 41 | +| 1 | 6 | |
| 42 | +| 2 | 6 | |
| 43 | +| 7 | 2 | |
| 44 | +| 8 | 3 | |
| 45 | +| 3 | 9 | |
| 46 | ++-------+-------+ |
| 47 | +<strong>Output:</strong> |
| 48 | ++-------+-----------------------+ |
| 49 | +| user1 | percentage_popularity | |
| 50 | ++-------+-----------------------+ |
| 51 | +| 1 | 55.56 | |
| 52 | +| 2 | 33.33 | |
| 53 | +| 3 | 33.33 | |
| 54 | +| 4 | 11.11 | |
| 55 | +| 5 | 11.11 | |
| 56 | +| 6 | 22.22 | |
| 57 | +| 7 | 11.11 | |
| 58 | +| 8 | 11.11 | |
| 59 | +| 9 | 11.11 | |
| 60 | ++-------+-----------------------+ |
| 61 | +<strong>Explanation:</strong> |
| 62 | +There are total 9 users on the platform. |
| 63 | +- User "1" has friendships with 2, 3, 4, 5 and 6. Therefore, the percentage popularity for user 1 would be calculated as (5/9) * 100 = 55.56. |
| 64 | +- User "2" has friendships with 1, 6 and 7. Therefore, the percentage popularity for user 2 would be calculated as (3/9) * 100 = 33.33. |
| 65 | +- User "3" has friendships with 1, 8 and 9. Therefore, the percentage popularity for user 3 would be calculated as (3/9) * 100 = 33.33. |
| 66 | +- User "4" has friendships with 1. Therefore, the percentage popularity for user 4 would be calculated as (1/9) * 100 = 11.11. |
| 67 | +- User "5" has friendships with 1. Therefore, the percentage popularity for user 5 would be calculated as (1/9) * 100 = 11.11. |
| 68 | +- User "6" has friendships with 1 and 2. Therefore, the percentage popularity for user 6 would be calculated as (2/9) * 100 = 22.22. |
| 69 | +- User "7" has friendships with 2. Therefore, the percentage popularity for user 7 would be calculated as (1/9) * 100 = 11.11. |
| 70 | +- User "8" has friendships with 3. Therefore, the percentage popularity for user 8 would be calculated as (1/9) * 100 = 11.11. |
| 71 | +- User "9" has friendships with 3. Therefore, the percentage popularity for user 9 would be calculated as (1/9) * 100 = 11.11. |
| 72 | +user1 is sorted in ascending order. |
| 73 | +</pre> |
| 74 | + |
| 75 | +## 解法 |
| 76 | + |
| 77 | +<!-- 这里可写通用的实现逻辑 --> |
| 78 | + |
| 79 | +<!-- tabs:start --> |
| 80 | + |
| 81 | +### **SQL** |
| 82 | + |
| 83 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 84 | + |
| 85 | +```sql |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +<!-- tabs:end --> |
0 commit comments