SQL A ,B 그룹별 A,B의 출력을 상위 2개만 출력하기
ROW_NUMBER() 응용예제
A ,B 그룹별 A,B의 출력을 상위 2개만 출력하기
SELECT A,B FROM
(
SELECT A, B,
ROW_NUMBER() OVER(PARTITION BY A ORDER BY B) rn
FROM TESTS
)
WHERE rn <= 2;
(
SELECT A, B,
ROW_NUMBER() OVER(PARTITION BY A ORDER BY B) rn
FROM TESTS
)
WHERE rn <= 2;
또는
SELECT A,B FROM
(
SELECT A,B,
ROW_NUMBER() OVER(PARTITION BY A ORDER BY A,B) RM FROM TESTS GROUP BY A,B
) WHERE RM <= 2;
(
SELECT A,B,
ROW_NUMBER() OVER(PARTITION BY A ORDER BY A,B) RM FROM TESTS GROUP BY A,B
) WHERE RM <= 2;
댓글
댓글 쓰기