2

I am interested in computing statistics over moving window ranges

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
user855
  • 19,048
  • 38
  • 98
  • 162

2 Answers2

0

I'm not sure what are you looking for, but regarding window functions both MySQL 8.0 and PostgreSQL 10 implement the same set of them. They both support PARTITION BY and ORDER BY clauses.

CUME_DIST()
DENSE_RANK()
FIRST_VALUE()
LAG()
LAST_VALUE()
LEAD()
NTH_VALUE()
NTILE()
PERCENT_RANK()
RANK()
ROW_NUMBER()

Click here for PostgreSQL

Click here for MySQL

Kamil Gosciminski
  • 16,547
  • 8
  • 49
  • 72
0

PostgreSQL 11 takes the lead again by implementing the lesser seen window frame unit GROUPS (in addition to ROWS and RANGE), as well as by adding support for the (in my opinion arcane) window frame exclusion clause.

An example of what's possible in PostgreSQL 11, but not MySQL 8 is this:

WITH t(v) AS (
  VALUES (1), (1), (3), (5), (5), (5), (6)
)
SELECT
  v,
  array_agg(v) OVER (o GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING EXCLUDE CURRENT ROW) AS current,
  array_agg(v) OVER (o GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING EXCLUDE GROUP) AS group,
  array_agg(v) OVER (o GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING EXCLUDE TIES) AS ties,
  array_agg(v) OVER (o GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING EXCLUDE NO OTHERS) AS no_others
FROM t
WINDOW o AS (ORDER BY v)

Yielding:

v |current     |group       |ties          |no_others     |
--|------------|------------|--------------|--------------|
1 |{1,3}       |{3}         |{1,3}         |{1,1,3}       |
1 |{1,3}       |{3}         |{1,3}         |{1,1,3}       |
3 |{1,1,5,5,5} |{1,1,5,5,5} |{1,1,3,5,5,5} |{1,1,3,5,5,5} |
5 |{3,5,5,6}   |{3,6}       |{3,5,6}       |{3,5,5,5,6}   |
5 |{3,5,5,6}   |{3,6}       |{3,5,6}       |{3,5,5,5,6}   |
5 |{3,5,5,6}   |{3,6}       |{3,5,6}       |{3,5,5,5,6}   |
6 |{5,5,5}     |{5,5,5}     |{5,5,5,6}     |{5,5,5,6}     |

I've blogged about these PostgreSQL features more in detail here.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509