rows/range:窗口子句,主要用来限定分组(也称窗口)的行数和数据范围。
窗口子句必须和order by 子句同时利用,如果指定了order by 子句未指定窗口子句,则默认为RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,即从当前分组出发点到当前行。
行比力分析函数lead和lag无窗口子句。 窗口子句常用语法:
CURRENT ROW:当前行
UNBOUNDED:无界限(出发点或终点)
PRECEDING:往前
FOLLOWING:以后
如上文《2.4,值函数》,如果想获取整个窗口的LAST_VALUE()和NTH_VALUE:
SELECT
*,
-- 获取第一行的score
FIRST_VALUE(score) OVER w AS `first`,
-- 获取最后一行score
LAST_VALUE(score) OVER w AS `last`,
-- 获取最后2行score
NTH_VALUE(score, 2) OVER w AS `second`,
-- 获取最后3行score
NTH_VALUE(score, 3) OVER w AS `third`
FROM `class`
WINDOW w AS (
PARTITION BY course
ORDER BY score DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)
复制代码
rows 和range区别:
rows是物理窗口,即根据order by 子句排序后,取的前N行及后N行的数据盘算(与当前行的值无关,只与排序后的行号干系)。
range是逻辑窗口,即根据order by 子句排序后,取的前N行及和当前行有雷同order by值的所有行数据盘算。
比方在《2.1,聚合函数》飘黄部分,由于默认窗口字句是RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,所以改成把"RANGE"改成"ROWS"就是逐条统计:
SELECT
*,
-- 默认RANGE
SUM(score) OVER w AS sum1,
-- 指定ROWS
SUM(score) OVER (w ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS sum2,
-- 默认RANGE
COUNT(score) OVER w AS count1,
-- 指定ROWS
COUNT(score) OVER (w ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS count2
FROM `class`
WINDOW w AS (PARTITION BY course ORDER BY score DESC)