Im between cherry

MySQL | ROW_NUMBER(), RANK(), DENSE_RANK() 윈도우 함수 본문

데이터분석/SQL

MySQL | ROW_NUMBER(), RANK(), DENSE_RANK() 윈도우 함수

meal 2020. 8. 30. 15:27

윈도우 함수에서만 가능한 데이터 순위 정하기

1. MySQL ROW_NUMBER Function

https://www.mysqltutorial.org/mysql-window-functions/mysql-row_number-function/

 

MySQL ROW_NUMBER and Its Useful Applications

In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.

www.mysqltutorial.org

SELECT 
	ROW_NUMBER() OVER (
		ORDER BY productName
	) row_num,
    productName,
    msrp
FROM 
	products
ORDER BY 
	productName;

 

2. MySQL RANK Function

https://www.mysqltutorial.org/mysql-window-functions/mysql-rank-function/

 

A Guide to MySQL RANK Funtion By Practical Examples

This tutorial introduces to the MySQL RANK function and how to apply it to assign the rank to each row within the partition of a result set.

www.mysqltutorial.org

RANK()함수는 결과 집합의 파티션 내 각 행에 순위를 할당합니다. 

행의 순위는 1과 그 앞에 오는 순위 수를 더한 값으로 지정됩니다.

SELECT
    val,
    RANK() OVER (
        ORDER BY val
    ) my_rank
FROM
    t;

 

 

3. MySQL DENSE_RANK Function

DENSE_RANK() 함수는 값 순위에 간격이 없도록 결과 집합 내의 각 행의 값에 순위를 매긴다.

행의 순위는 행 앞에 오는 고유한 순위 값의 수에서 1 씩 증가합니다.

SELECT
    val,
    DENSE_RANK() OVER (
        ORDER BY val
    ) my_rank
FROM
    t;

Comments