我对计算移动窗口范围内的统计数据感兴趣
答案 0 :(得分:0)
我不确定您在寻找什么,但是关于窗口函数,MySQL 8.0和PostgreSQL 10都实现了它们的相同集合。它们都支持PARTITION BY
和ORDER BY
子句。
CUME_DIST()
DENSE_RANK()
FIRST_VALUE()
LAG()
LAST_VALUE()
LEAD()
NTH_VALUE()
NTILE()
PERCENT_RANK()
RANK()
ROW_NUMBER()
答案 1 :(得分: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.