[MDEV-14917] Problem with MEDIAN() function and using ORDER BY Created: 2018-01-11  Updated: 2018-01-12  Resolved: 2018-01-12

Status: Closed
Project: MariaDB Server
Component/s: Optimizer - Window functions
Affects Version/s: 10.3.3
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Paul Arch Assignee: Varun Gupta (Inactive)
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

Ubuntu 16.04 LTS, Virtual Machine 4 cores / 8GB RAM
4.4.0-108-generic #131-Ubuntu SMP Sun Jan 7 14:34:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux



 Description   

https://gist.github.com/paul78oz/4ea9f626ae10333a5abaacf828804f94

CREATE TABLE book_rating (id TINYINT, name CHAR(30), star_rating TINYINT);
 
INSERT INTO book_rating VALUES (1, 'Lord of the Ladybirds', 5);
INSERT INTO book_rating VALUES (2, 'Lord of the Ladybirds', 3);
INSERT INTO book_rating VALUES (3, 'Lady of the Flies', 1);
INSERT INTO book_rating VALUES (4, 'Lady of the Flies', 2);
INSERT INTO book_rating VALUES (5, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (6, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (7, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (8, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (9, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (10, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (11, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (12, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (13, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (14, 'Lady of the Flies', 5);
INSERT INTO book_rating VALUES (15, 'Lady of the Flies', 5);
 
// This works as expected
SELECT name, median(star_rating) OVER (PARTITION BY name) FROM book_rating;
 
// This doesn't work
SELECT name, median(star_rating) OVER (ORDER by id ROWS 3 preceding) from book_rating
 
// This works as expected
SELECT name, avg(star_rating) OVER (ORDER by id ROWS 3 preceding) from book_rating

As per expanded example from documentation and attached, cannot seem to get working MEDIAN using ORDER BY:

eg. SELECT name, median(star_rating) OVER (ORDER by id ROWS 3 preceding) from book_rating

Results in:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER by id ROWS 3 preceding) from book_rating

No errors showing in mariadb logs.



 Comments   
Comment by Varun Gupta (Inactive) [ 2018-01-12 ]

archio
Median functions are a special set of window function , they don't support frame definitions.
Secondly , the order by clause is also not allowed in the OVER clause, the order by is done only with the parameter provided to the MEDIAN function.

Comment by Paul Arch [ 2018-01-12 ]

OK - looks like the main documentation for the function has now been updated to reflect this, at time of bug report it listed 'ORDER by LIST' in syntax

Frame definitions would be nice, to support a 'rolling' median

Thanks !

Generated at Thu Feb 08 08:17:16 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.