OceanBase Oracle 租户支持一些复杂的分析函数,分析函数通常用于计算累积、移动、居中和报告汇总。OceanBase Oracle 租户支持的分析函数如下表所示:
函数名 | 功能描述 |
AVG | 求平均 |
SUM | 求和 |
MAX | 求最大 |
MIN | 求最小 |
COUNT | 计数 |
CUME_DIST | 求累积分布 |
RANK | 求排名(不连续) |
DENSE_RANK | 求行组中行的秩 |
PEERCENT_RANK | 求累积分布值(百分比) |
FIRST_VALUE | 求有序值中第一个值 |
LAST_VALUE | 求有序值中最后一个值 |
LAG |
|
LEAD | 对一组行的集合进行计算并返回多个值 |
LISTAGG | 列转行 |
NTH_VALUE | 求窗口中第N行满足表达式的值 |
NTILE | 对序号进行分组处理 |
RATIO_TO_REPORT | 计算一个值与一组值之和的比率 |
ROW_NUMBER | 为每一行分配一个不重复的数字 |
STDDEV | 求总体标准差 |
STDDEV_POP | 求总体标准差 |
STDDEV_SAMP | 求样本标准差 |
VARIANCE | 求指定参数列的方差 |
复杂的分析函数语法格式简单描述如下:
analytic_function ( [ arguments ] )
OVER
(
[
PARTITION BY { expr [, expr] … }
[
ORDER [ SIBLINGS ] BY
{ expr | position | c_alias }
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
[, { expr | position | c_alias }
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
] …
[
{ ROWS | RANGE }
{ BETWEEN
{ UNBOUNDED PRECEDING | CURRENT ROW | value_expr
{ PRECEDING | FOLLOWING }
} AND
{ UNBOUNDED FOLLOWING | CURRENT ROW | value_expr { PRECEDING | FOLLOWING }
}
|
{ UNBOUNDED PRECEDING | CURRENT ROW| value_expr PRECEDING
}
}
]
]
)
示例:用分析函数查询
SELECT * FROM (
SELECT o_w_id, o_d_id, o_ol_cnt, o_entry_d, rank() OVER (PARTITION BY o_w_id order BY o_entry_d) entry_order
FROM ordr
ORDER BY o_w_id , entry_order
) WHERE entry_order <=5;
SELECT * FROM (
SELECT o_w_id, o_d_id, o_ol_cnt, o_entry_d, dense_rank() OVER (PARTITION BY o_w_id order BY o_entry_d) entry_order
FROM ordr
ORDER BY o_w_id , entry_order
) WHERE entry_order <=5;
SELECT o_w_id, o_d_id, o_ol_cnt, o_entry_d, first_value(o_c_id) OVER (PARTITION BY o_w_id order BY o_entry_d) first_cid
FROM ordr
ORDER BY o_w_id ;