Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet Code] 1633. Percentage of Users Attended a Contest

์ธ” 2023. 10. 23. 21:34

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ 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;