[Leet Code] 1633. Percentage of Users Attended a Contest
๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ๋ฌธ์
https://leetcode.com/problems/percentage-of-users-attended-a-contest/description/
Percentage of Users Attended a Contest - LeetCode
Can you solve this real interview question? Percentage of Users Attended a Contest - Table: Users +-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | user_name | varchar | +-------------+---------+ user_id is the
leetcode.com
Table : Users
Table : Register
Write a solution to find the percentage of the users registered in each contest rounded to two decimals.
Return the result table ordered by percentage in descending order.
In case of a tie, order it by contest_id in ascending order.
…
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
๊ฐ ์ฝํ
์คํธ์ ๋ฑ๋ก๋ ์ ์ ์ ๋น์จ์ ์์์ ์ดํ ๋ ์๋ฆฌ๊น์ง ๋ฐ์ฌ๋ฆผํ์ฌ ๊ตฌํด์ผ ํ๋ค.
๊ฒฐ๊ณผ๋ percentage ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ , ๋์ ์ธ ๊ฒฝ์ฐ์๋ Contest_id์์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค.
์ ์ ์ ๋น์จ์ Users ํ ์ด๋ธ ๋ฐ์ดํฐ๊ฐ(์ ์ ์)๋ฅผ ์ง๊ณํด์ ๋ถ๋ชจ๋ก ←( SELECT์ ์ ์๋ธ์ฟผ๋ฆฌ; ์ค์นผ๋ผ์๋ธ์ฟผ๋ฆฌ )
์ถ๋ ฅํ๋ค.
์ฆ, ์ ์ฒด ์ ์ ์๋ง Users ํ ์ด๋ธ์์ ๊ฐ์ ธ์ค๊ณ ๋๋จธ์ง๋ Register ํ ์ด๋ธ์์ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ๋ฉด ๊ทธ๋ฃนํ์ ํตํด ๋ฑ๋ก ์ ์ ๋น์จ์ ์๋์ ๊ฐ์ด ์ถ๋ ฅํ ์ ์์๋ค.
SELECT contest_id,
ROUND(COUNT(user_id) / (SELECT COUNT(*) FROM Users) * 100, 2) AS percentage
FROM Register
GROUP BY 1
ORDER BY 2 DESC, 1;