https://leetcode.com/problems/department-top-three-salaries/
Department Top Three Salaries - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
위의 문제를 풀다 RANK에서 그룹별로 처리하는 방법을 알게 되어 정리.
결론은 그룹별로 해당 Row의 Rank를 매기고 싶을 때 PARTITION BY를 사용하면 된다.
SELECT Department, Employee, salary FROM
(
SELECT
Department.name AS Department,
Employee.name AS Employee,
salary,
DENSE_RANK() OVER (PARTITION BY departmentId ORDER BY salary DESC) AS RANKING
FROM Employee JOIN Department
ON Employee.departmentId = Department.id
) SUB
WHERE RANKING <= 3
;
departmentId(부서 id) 그룹으로 RANK를 매길건데, salary를 높은 것부터
RANK 사용법은 아래 참고
2022.03.31 - [Database/MySQL] - [MySQL] rank함수 - 랭킹
[MySQL] rank함수 - 랭킹
https://leetcode.com/problems/rank-scores/ Rank Scores - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next i..
hkim-data.tistory.com
'Database > MySQL' 카테고리의 다른 글
[MySQL] DATE 타입으로 변경 DATE_FORMAT (0) | 2022.04.02 |
---|---|
[MySQL] 데이터 삭제 (0) | 2022.03.31 |
[MySQL] rank함수 - 랭킹 (0) | 2022.03.31 |
[MySQL] 변수 처리(local variable vs. user variable) (0) | 2022.03.31 |
[MySQL] N번째로 큰 값 (0) | 2022.03.31 |
댓글