Syntax
NTILE(expr) OVER ([ query_partition_clause ] order_by_clause)
Purpose
You can call this function to divide sorted rows of a partition into a specified number of groups that have the same size and return the group number to each row. If expr is NULL, NULL is returned.
Examples
obclient> CREATE TABLE employees(LAST_NAME CHAR(10), SALARY DECIMAL, JOB_ID CHAR(32));
INSERT INTO employees VALUES('JIM', 2000, 'CLEANER');
INSERT INTO employees VALUES('MIKE', 12000, 'ENGINEERING');
INSERT INTO employees VALUES('LILY', 13000, 'ENGINEERING');
INSERT INTO employees VALUES('TOM', 11000, 'ENGINEERING');
COMMIT;
Query OK, 0 rows affected (0.00 sec)
obclient> SELECT LAST_NAME, NTILE(10) OVER(PARTITION BY JOB_ID ORDER BY SALARY) NTL FROM employees;
+-----------+------+
LAST_NAME NTL
+-----------+------+
JIM 1
TOM 1
MIKE 2
LILY 3
+-----------+------+
4 rows in set (0.00 sec)