Skip to content

Instantly share code, notes, and snippets.

@paul78oz
Created January 11, 2018 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul78oz/4ea9f626ae10333a5abaacf828804f94 to your computer and use it in GitHub Desktop.
Save paul78oz/4ea9f626ae10333a5abaacf828804f94 to your computer and use it in GitHub Desktop.
Test of MariaDB 10.3.3 median() function
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment