Database/SQL Server
SQL Server - NTILE 함수
청렴결백한 만능 재주꾼
2020. 12. 18. 00:34
반응형
NTILE function
- SQL Server 2005에 소개 되어진 함수
- ORDER BY 절이 필수로 요구된다. ORDER BY clause is required
- PARTITION BY 절은 선택적으로 요구된다. PARTITION BY clause is optional
- 지정한 숫자만큼의 그룹으로 행을 나눈다. Distributes the rows into a specified number of groups.
- 만약 지정한 숫자가 정확하게 나누어지지 않는 값이라면 그룹의 크기가 다를 수 있다. If the number of rows is not divisible by number of groups, you may have groups of two different sizes.
- 이런 경우엔 큰 그룹이 먼저 온다. Larger groups come before smaller groups
Syntax:
NTILE (Number_of_Groups) OVER (ORDER BY Col1, Col2, ...)
SELECT Name, Gender, Salary,
NTILE(3) OVER (ORDER BY Salary) AS [Ntile]
FROM Employees
NTILE 3으로 하고 파티션을 넣을 경우
SELECT Name, Gender, Salary,
NTILE(3) OVER (PARTITION BY Gender ORDER BY Salary) AS [Ntile]
FROM Employees
반응형