Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTILE window function returns incorrect results #6829

monetdb-team opened this issue Nov 30, 2020 · 0 comments

NTILE window function returns incorrect results #6829

monetdb-team opened this issue Nov 30, 2020 · 0 comments


Copy link

@monetdb-team monetdb-team commented Nov 30, 2020

Date: 2020-02-21 10:25:12 +0100
From: @bernardom29
To: SQL devs <>
Version: -- development
CC: @PedroTadim

Last updated: 2020-02-24 09:32:58 +0100

Comment 27576

Date: 2020-02-21 10:25:12 +0100
From: @bernardom29

The NTILE window function is returning incorrect results in some cases. In the example below, the function divides the result set in more buckets than specified in the function argument.

How to reproduce:
1 - Create table and add values

create table employees (dep varchar(10), name varchar(20), salary double, hire_date date);
insert into employees values
('mgmt', 'Smith',81,str_to_date('10-08-2019', '%d-%m-%Y')),
('dev', 'Jones',55,str_to_date('9-08-2019', '%d-%m-%Y')),
('sls', 'Williams',55,str_to_date('14-07-2019', '%d-%m-%Y')),
('sls', 'Taylor',62,str_to_date('13-08-2019', '%d-%m-%Y')),
('dev', 'Brown',62,str_to_date('6-06-2019', '%d-%m-%Y')),
('mgmt', 'Davies',84,str_to_date('4-06-2019', '%d-%m-%Y')),
('sls', 'Evans',87,str_to_date('1-09-2019', '%d-%m-%Y')),
('sls', 'Wilson',72,str_to_date('21-09-2019', '%d-%m-%Y')),
('sls', 'Thomas',72,str_to_date('12-06-2019', '%d-%m-%Y')),
('mgmt', 'Johnson',100,str_to_date('12-07-2019', '%d-%m-%Y'));

2 - Execute this query, which gives incorrect results

SELECT dep, name, salary, NTILE(4) OVER (ORDER BY hire_date) FROM employees;

It should divide the 10 rows into 4 buckets, but it returns a 5 for the last row, which is incorrect.
The function returns correct results in the case of 2 buckets, for example.

Comment 27577

Date: 2020-02-21 11:19:22 +0100
From: MonetDB Mercurial Repository <>

Changeset 670675a1ce57 made by Pedro Ferreira in the MonetDB repo, refers to this bug.

For complete details, see https//devmonetdborg/hg/MonetDB?cmd=changeset;node=670675a1ce57

Changeset description:

Added test and fix for bug #6829. Changed ntile implementation for a more generic one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant