반응형
https://leetcode.com/problems/percentage-of-users-attended-a-contest/description/
group by와 집계함수를 사용해본 문제였습니다.
📕 풀이방법
📔 풀이과정
두 table을 join하지 않고 contest_id를 group by로 percentage를 계산해 select해줍니다.
📔 정답 출력 | 반환
📕 Code
📔 ANSI SQL
SELECT
r.contest_id,
ROUND(COUNT(DISTINCT r.user_id) / (SELECT COUNT(*) FROM Users) * 100.0, 2) AS percentage
FROM
Register r
GROUP BY
r.contest_id
ORDER BY
percentage DESC,
r.contest_id ASC;
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > SQL' 카테고리의 다른 글
(SQL) - LeetCode (easy) 1795. Rearrange Products Table (0) | 2024.08.07 |
---|---|
(SQL) - LeetCode (easy) 1731. The Number of Employees Which Report to Each Employee (0) | 2024.06.26 |
(SQL) - LeetCode (easy) 1527. Patients With a Condition (0) | 2024.04.19 |
(SQL) - LeetCode (easy) 1507. Reformat Date (0) | 2024.04.13 |
(SQL) - LeetCode (easy) 1484. Group Sold Products By The Date (0) | 2024.04.02 |