728x90
< 문제 >
[SQL50] 1633. Percentage of Users Attended a Contest (AGGREGATE)
< 풀이 >
SELECT contest_id,
ROUND(COUNT(DISTINCT user_id) / (SELECT COUNT(1) FROM Users)::decimal * 100, 2) AS percentage
FROM Register
GROUP BY contest_id
ORDER BY percentage DESC, contest_id;
Decimal
- decimal : 고정 소수점(fixed-point) 숫자형 타입
- integer, float와 달리 정확한 소수 연산이 가능하다.
- decimal(p, s)
- p : 전체 자릿수(precision, 최대 131072자리)
- s : 소수점 이하 자릿수(scale, 최대 16383자리)
- ::decimal
- COUNT(1) 결과를 decimal 타입으로 변환한다. (정수(int) → 소수(decimal))
- 예시 ) 100::decimal → 100.0 / 0::decimal → 0.0
728x90
'coding_test > SQL 문제' 카테고리의 다른 글
[LeetCode] 1193. Monthly Transactions I (AGGREGATE) (0) | 2025.02.19 |
---|---|
[LeetCode] 1211. Queries Quality and Percentage (AGGREGATE) (0) | 2025.02.11 |
[LeetCode] 1075. Project Employees I (AGGREGATE) (0) | 2025.02.10 |
[LeetCode] 1251. Average Selling Price (AGGREGATE) (0) | 2025.02.10 |
[LeetCode] 620. Not Boring Movies (AGGREGATE) (0) | 2025.02.10 |