> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ntile 윈도 함수 문서

# ntile

파티션 내에서 정렬된 행을 지정된 개수의 버킷(그룹)으로 가능한 한 균등하게 나누고, 현재 행이 속한 버킷 번호를 반환합니다. 버킷 번호는 1부터 시작합니다. 각 파티션에서 행은 순서대로 버킷에 할당됩니다. 행 수가 버킷 수로 나누어떨어지지 않으면 앞쪽 버킷에는 뒤쪽 버킷보다 행이 1개 더 할당됩니다.

**구문**

```sql theme={null}
ntile (buckets)
  OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column]
        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING] | [window_name])
FROM table_name
WINDOW window_name as ([PARTITION BY grouping_column] [ORDER BY sorting_column] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
```

인수 `buckets`는 상수인 양의 정수여야 합니다.

`ORDER BY` 절이 필요합니다. 윈도우 프레임은 전체 파티션(`ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`)이어야 합니다. 이는 명시적으로 지정하지 않으면 기본으로 사용되는 프레임이기도 합니다.

윈도우 함수 구문에 대한 자세한 내용은 [윈도우 함수 - 구문](/docs/ko/reference/functions/window-functions/index#syntax)을 참조하십시오.

**반환 값**

* 현재 행이 해당 파티션 내에서 속하는 버킷 번호입니다. [UInt64](/docs/ko/reference/data-types/int-uint).

**예시**

다음 예시는 플레이어를 급여 내림차순으로 정렬한 4개의 버킷으로 나눕니다.

```sql title="Query" theme={null}
CREATE TABLE salaries
(
    `team` String,
    `player` String,
    `salary` UInt32,
    `position` String
)
Engine = Memory;

INSERT INTO salaries FORMAT Values
    ('Port Elizabeth Barbarians', 'Gary Chen', 195000, 'F'),
    ('New Coreystad Archdukes', 'Charles Juarez', 190000, 'F'),
    ('Port Elizabeth Barbarians', 'Michael Stanley', 150000, 'D'),
    ('New Coreystad Archdukes', 'Scott Harrison', 150000, 'D'),
    ('Port Elizabeth Barbarians', 'Robert George', 195000, 'M'),
    ('South Hampton Seagulls', 'Douglas Benson', 150000, 'M'),
    ('South Hampton Seagulls', 'James Henderson', 140000, 'M');
```

```sql title="Query" theme={null}
SELECT player, salary,
       ntile(4) OVER (ORDER BY salary DESC, player ASC) AS bucket
FROM salaries;
```

```response title="Response" theme={null}
   ┌─player──────────┬─salary─┬─bucket─┐
1. │ Gary Chen       │ 195000 │      1 │
2. │ Robert George   │ 195000 │      1 │
3. │ Charles Juarez  │ 190000 │      2 │
4. │ Douglas Benson  │ 150000 │      2 │
5. │ Michael Stanley │ 150000 │      3 │
6. │ Scott Harrison  │ 150000 │      3 │
7. │ James Henderson │ 140000 │      4 │
   └─────────────────┴────────┴────────┘
```

여기서는 행이 7개이고 버킷이 4개이므로, 앞의 3개 버킷에는 각각 행이 2개씩 들어 있고 마지막 버킷에는 행이 1개만 들어 있습니다.
