sql_코드카타(2024.02.14)

김수경·2024년 2월 14일

코드카타

목록 보기
26/29

Percentage of Users Attended a Contest

Table 1 : User

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| user_name | varchar |
+-------------+---------+
user_id is the primary key (column with unique values) for this table.
Each row of this table contains the name and the id of a user.

Table 2 : Register

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| contest_id | int |
| user_id | int |
+-------------+---------+

문제

각 콘테스트에 등록된 사용자의 비율을 소수점 이하 두 자리 까지 반올림하여 구하는 솔루션을 작성하세요.
percentage로 내림차순으로 반환합니다 . 동점인 경우 contest_id 오름차순으로 정렬 합니다.

👩🏻‍💻my coding

# 콘테스트별 참여 유저
SELECT contest_id, COUNT(DISTINCT user_id) AS users_participated
FROM Register
GROUP BY contest_id;

# 유저의 총 합
SELECT COUNT(DISTINCT user_id) AS total_users
FROM Register;

# 합치기
select contest_id,
round(count(distinct user_id) *100 / (select count(distinct user_id) as total_users from register),2) as percentage
from register
group by contest_id 
order by percentage desc, contest_id 
profile
잘 하고 있는겨?

0개의 댓글