📜  聚合和窗口函数

📅  最后修改于: 2020-12-02 06:07:38             🧑  作者: Mango


本章详细说明了聚合和窗口功能。

汇总功能

集合函数从一组输入值产生单个结果。下表详细描述了聚合函数列表。

S.No. Function & Description
1 AVG(exp)

Averages a column of all records in a data source.

2 CORR(expression1, expression2)

Returns the coefficient of correlation between a set of number pairs.

3 COUNT()

Returns the number rows.

4 MAX(expression)

Returns the largest value of the selected column.

5 MIN(expression)

Returns the smallest value of the selected column.

6 SUM(expression)

Returns the sum of the given column.

7 LAST_VALUE(expression)

Returns the last value of the given column.

视窗功能

Window函数在一组行上执行,并为查询中的每一行返回一个值。术语窗口具有该函数的行集的含义。

查询中的Window函数使用OVER()子句定义窗口。

OVER()子句具有以下功能-

  • 定义窗口分区以形成行组。 (PARTITION BY子句)
  • 对分区内的行进行排序。 (ORDER BY子句)

下表详细描述了窗口功能。

Function Return type Description
rank() int Returns rank of the current row with gaps.
row_num() int Returns the current row within its partition, counting from 1.
lead(value[, offset integer[, default any]]) Same as input type Returns value evaluated at the row that is offset rows after the current row within the partition. If there is no such row, default value will be returned.
lag(value[, offset integer[, default any]]) Same as input type Returns value evaluated at the row that is offset rows before the current row within the partition.
first_value(value) Same as input type Returns the first value of input rows.
last_value(value) Same as input type Returns the last value of input rows.