rubus0304 님의 블로그
[데이터분석] 코드카타 94 본문
https://leetcode.com/problems/percentage-of-users-attended-a-contest/description/
94. Percentage of Users Attended a Contest
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.
The result format is in the following example.
(오답) % 구하려면 각 sum 한 걸 나눠야하나..?
select r.contest_id,
round(count(u.user_id)/count(r.user_id),2) percentage
from Users u join Register r on u.user_id = r.user_id
group by 1
order by 2 desc, 1
(오답) % 를 구하기 위해 *100 추가.
select r.contest_id,
round(count(u.user_id)/count(r.user_id)*100,2) percentage
from Users u join Register r on u.user_id = r.user_id
group by 1
order by 2 desc, 1
(정답) 왜 Users 테이블에서 전체 count 를 한 값으로 나누는 것인지 모르겠다..!
select r.contest_id,
round(count(u.user_id)/(select count(*) from Users)*100,2) percentage
from Users u join Register r on u.user_id = r.user_id
group by 1
order by 2 desc, 1

'Data Analyst > daily' 카테고리의 다른 글
[데이터분석] 코드카타 96 (3) | 2024.11.05 |
---|---|
[데이터분석] 코드카타 95 (3) | 2024.11.04 |
[데이터분석] 코드카타 93 (0) | 2024.10.31 |
[데이터분석] 코드카타 92 (0) | 2024.10.30 |
[데이터분석] 코드카타 91 (0) | 2024.10.29 |