NTILE in the China-domestically developed GBase 8a MPP database from GBASE splits the rows of a window into a given number of buckets. When the rows do not divide evenly, the earlier buckets get the extra rows. This post walks through the syntax and a set of runnable examples.
Syntax
NTILE(num) OVER([PARTITION BY col_name1,col_name2,…] ORDER BY col_name1 [asc/desc], col_name2 [asc/desc],…)
Parameters
- num: how many buckets to split into. The first bucket is numbered 1, the second 2, and so on.
- If the rows in a window cannot be divided evenly by num, the earlier buckets are filled first.
Examples
Sample data
gbase> create table td(id int, type int,val decimal(20,3));
Query OK, 0 rows affected (Elapsed: 00:00:00.17)
gbase> insert into td values(1,1,111),(1,1,222),(2,1,333),(2,2,444),(3,2,555),(3,2,666),(4,2,777);
Query OK, 7 rows affected (Elapsed: 00:00:00.12)
Records: 7 Duplicates: 0 Warnings: 0
gbase> insert into td values(5,1,null),(50,2,null);
Query OK, 2 rows affected (Elapsed: 00:00:00.09)
Records: 2 Duplicates: 0 Warnings: 0
gbase> select * from td;
+------+------+---------+
gbase> select * from td order by type;
+------+------+---------+
| id | type | val |
+------+------+---------+
| 1 | 1 | 222.000 |
| 2 | 1 | 333.000 |
| 5 | 1 | NULL |
| 1 | 1 | 111.000 |
| 3 | 2 | 666.000 |
| 4 | 2 | 777.000 |
| 3 | 2 | 555.000 |
| 2 | 2 | 444.000 |
| 50 | 2 | NULL |
+------+------+---------+
9 rows in set (Elapsed: 00:00:00.04)
Standard NTILE
Splitting each type partition into 2 buckets: the type=2 group has 5 rows, so bucket 1 takes 3 and bucket 2 takes 2.
gbase> select *,ntile(2)over(partition by type order by id) maxOfType from td;
+------+------+---------+-----------+
| id | type | val | maxOfType |
+------+------+---------+-----------+
| 1 | 1 | 111.000 | 1 |
| 1 | 1 | 222.000 | 1 |
| 2 | 1 | 333.000 | 2 |
| 2 | 2 | 444.000 | 1 |
| 3 | 2 | 555.000 | 1 |
| 3 | 2 | 666.000 | 1 |
| 4 | 2 | 777.000 | 2 |
| 5 | 1 | NULL | 2 |
| 50 | 2 | NULL | 2 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.35)
NTILE without PARTITION BY
All rows are split into 2 buckets.
gbase> select *,ntile(2)over(order by id) maxOfType from td;
+------+------+---------+-----------+
| id | type | val | maxOfType |
+------+------+---------+-----------+
| 1 | 1 | 111.000 | 1 |
| 1 | 1 | 222.000 | 1 |
| 2 | 1 | 333.000 | 1 |
| 2 | 2 | 444.000 | 1 |
| 3 | 2 | 555.000 | 1 |
| 3 | 2 | 666.000 | 2 |
| 4 | 2 | 777.000 | 2 |
| 5 | 1 | NULL | 2 |
| 50 | 2 | NULL | 2 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.08)
NTILE without ORDER BY
Natural order is used and is not guaranteed to be the same on every run.
gbase> select *,ntile(2)over(partition by type) maxOfType from td;
+------+------+---------+-----------+
| id | type | val | maxOfType |
+------+------+---------+-----------+
| 1 | 1 | 111.000 | 2 |
| 1 | 1 | 222.000 | 1 |
| 2 | 1 | 333.000 | 1 |
| 2 | 2 | 444.000 | 2 |
| 3 | 2 | 555.000 | 1 |
| 3 | 2 | 666.000 | 1 |
| 4 | 2 | 777.000 | 1 |
| 5 | 1 | NULL | 2 |
| 50 | 2 | NULL | 2 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.07)
NTILE with neither PARTITION BY nor ORDER BY
gbase> select *,ntile(2)over() maxOfType from td;
+------+------+---------+-----------+
| id | type | val | maxOfType |
+------+------+---------+-----------+
| 1 | 1 | 111.000 | 1 |
| 1 | 1 | 222.000 | 1 |
| 2 | 1 | 333.000 | 1 |
| 2 | 2 | 444.000 | 1 |
| 3 | 2 | 555.000 | 1 |
| 3 | 2 | 666.000 | 2 |
| 4 | 2 | 777.000 | 2 |
| 5 | 1 | NULL | 2 |
| 50 | 2 | NULL | 2 |
+------+------+---------+-----------+
9 rows in set (Elapsed: 00:00:00.29)
Going forward, NTILE is handy for quartile and decile reports, but the bucket number is only meaningful with a deterministic ORDER BY: without one, a gbase query may assign rows differently between runs of the same database, and that is easy to miss in a gbase cluster where data is scanned in parallel. Pin the sort keys, verify the bucket sizes, then build the ranking logic on top.
Top comments (0)