上云无忧 > 文档中心 > 百度数据仓库 Palo SQL语法手册 - 聚合函数
百度数据仓库 Palo Doris版
百度数据仓库 Palo SQL语法手册 - 聚合函数

文档简介:
聚合函数的行为是将多行的结果聚合成一行。 PALO 支持以下聚合函数: 1.avg 2.count 3.approx_count_distinct 4.max 5.min 6.sum 7.group_concat 8.variance_samp 9.variance_pop 10.percentile_approx 11.topn
*此产品及展示信息均由百度智能云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

聚合函数的行为是将多行的结果聚合成一行。

PALO 支持以下聚合函数:

1.avg
2.count
3.approx_count_distinct
4.max
5.min
6.sum
7.group_concat
8.variance_samp
9.variance_pop
10.percentile_approx
11.topn

BITMAP 和 HLL 类型相关的聚合函数,请参阅 BITMAP函数 以及 HLL函数。

AVG

Description

avg(numeric val)
  • 功能:该聚合函数返回集合中的平均数。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL值的行将被忽略。如果该表是空的或者AVG 的参数都是NULL,则该函数返回NULL。当查询指定使用 GROUP BY 从句时,则每个 GROUP BY 的值都会返回1条结果。
  • 返回类型: double类型

Example

mysql> select ss_ticket_number, avg(ss_sales_price) from store_sales group by ss_ticket_number; 
+------------------+-----------------------+ | ss_ticket_number | avg(`ss_sales_price`) | +-----
-------------+-----------------------+ | 28818 | 41.041875 | +------------------+-----------------------+

Keywords

avg

COUNT

Description

count([distinct] col_name...)
  • 功能:该聚合函数返回满足要求的行的数目,或者非NULL行的数目。

    • COUNT(*) 会计算包含NULL 值的行。
    • COUNT(column_name)仅会计算非NULL值的行。
    • 用户可以同时使用COUNT函数和DISTINCT操作符,count(distinct col_name...)会先对数据去重,然后再计算多个列的组合出现的次数。
  • 返回类型:int类型

Example

mysql> select count(distinct tiny_column, short_column) from small_table; +-----------------
------------------------------+ | count(DISTINCT `tiny_column`, `short_column`) | +--------
---------------------------------------+ | 2 | +-----------------------------------------------+

Keywords

count

APPROX_COUNT_DISTINCT

Description

approx_count_distinct(type col)
  • 功能:该聚合函数返回指定列的去重值。不同于 count(distinct) 方法,该函数使用 HyperLogLog 算法返回有误差的去重值,但效率比 count(distinct) 高。
  • 返回类型:int类型

Example

mysql> select approx_count_distinct(ss_ticket_number) from store_sales; +----------------------
---------------------+ | approx_count_distinct(`ss_ticket_number`) | +------------------------
-------------------+ | 46580 | +-------------------------------------------+

Keywords

approx_count_distinct

MAX

Description

max(type col)
  • 功能:该聚合函数返回集合中的最大值。该函数和min函数的功能相反。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL值的行将被忽略。如果该表是空的或者MAX的参数都是NULL,则该函数返回NULL。当查询指定使用 GROUP BY 从句时,则每个 GROUP BY 的值都会返回1条结果。
  • 返回类型:和输入参数相同的类型。

Example

mysql> select ss_ticket_number, max(ss_sales_price) from store_sales group by ss_ticket_number; +---
---------------+-----------------------+ | ss_ticket_number | max(`ss_sales_price`) | +----------
--------+-----------------------+ | 28818 | 136.42 | +------------------+-----------------------+

Keywords

max

MIN

Description

min(type col)
  • 功能:该聚合函数返回集合中的最小值。该函数和max函数的功能相反。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL 值的行将被忽略。如果该表是空的或者MIN 的参数都是NULL,则该函数返回NULL。当查询指定使用 GROUP BY 从句时,则每个 GROUP BY 的值都会返回1条结果。
  • 返回类型:和输入参数相同的类型。

Example

mysql> select ss_ticket_number, min(ss_sales_price) from store_sales group by ss_ticket_number; +-------
-----------+-----------------------+ | ss_ticket_number | min(`ss_sales_price`) | +---------------
---+-----------------------+ | 28818 | 0.91 | +------------------+-----------------------+

Keywords

min

SUM

Description

sum(numeric col)
  • 功能:该聚合函数返回集合中所有值的和。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL值的行将被忽略。如果该表是空的或者MIN的参数都是NULL,则该函数返回NULL。当查询指定使用GROUP BY从句时,则每个GROUP BY` 的值都会返回1条结果。
  • 返回类型:如果参数整型,则返回BIGINT,如果参数是浮点型则返回double类型

Example

mysql> select ss_ticket_number, sum(ss_sales_price) from store_sales group by ss_ticket_number; +-------
-----------+-----------------------+ | ss_ticket_number | sum(`ss_sales_price`) | +----------------
--+-----------------------+ | 28818 | 1971980.01 | +------------------+-----------------------+

Keywords

sum

GROUP_CONCAT

Description

group_concat(col[, separator])
  • 功能:该聚合函数会返回1个字符串,该字符串是集合中所有字符串连接起来形成的新字符串。如果用户指定分隔符,则分隔符用来连接两个相邻行的字符串。
  • 返回类型:string类型
  • 使用说明:默认情况下,该函数返回1个覆盖所有结果集的字符串。当查询指定使用group by 从句时,则每个group by的值都会返回1条结果。

Example

mysql> select * from tbl; +------+------+ | k1 | v1 | +------+------+ 
| 1 | a | | 1 | b | | 1 | c | +------+------+ mysql> select k1, group_concat(v1) 
from tbl group by k1; +------+--------------------+ | k1 | group_concat(`v1`) | +------+--------
------------+ | 1 | a, b, c | +------+--------------------+ mysql> select k1, group_concat(v1, '|')
 from tbl group by k1; +------+-------------------------+ | k1 | group_concat(`v1`, '|') | +----
--+-------------------------+ | 1 | a|b|c | +------+-------------------------+

Keywords

group_concat

VARIANCE,VAR_SAMP,VARIANCE_SAMP

Description

variance(numeric val) var_samp(numeric val) variance_samp(numeric val)
  • 功能:该类聚合函数返回一组数的样本方差。这是一个数学属性,它表示值与平均值之间的距离。它作用于数值类型。variance 和 var_samp 是 variance_samp 的别名。
  • 返回类型:double类型

Example

mysql> select ss_ticket_number, variance(ss_sales_price) from store_sales group by ss_ticket_number; +----
--------------+----------------------------+ | ss_ticket_number | variance(`ss_sales_price`) | +-------
-----------+----------------------------+ | 28818 | 1378.408511209
 | +------------------+----------------------------+

Keywords

variance, var_samp, variance_samp

VAR_POP,VARIANCE_POP

Description

var_pop(numeric val) variance_pop(numeric val)
  • 功能:该类聚合函数返回一组数的总体方差。这是一个数学属性,它表示值与平均值之间的距离。它作用于数值类型。var_pop 是 variance_pop 的别名。
  • 返回类型:double类型

Example

mysql> select ss_ticket_number, variance_pop(ss_sales_price) from store_sales group by ss_ticket_number;
 +------------------+--------------------------------+ | ss_ticket_number | variance_pop(`ss_sales_price`) 
| +------------------+--------------------------------+ | 28818 | 1378.408511134 | +---------------
---+--------------------------------+

Keywords

var_pop, variance_pop

PERCENTILE_APPROX

Description

percentile_approx(numeric val, double percentile, double compression)
  • 功能:该类聚合函数采用 T-Digest 算法,返回一组数的指定分位值的近似值。分位值 percentile 需在 0-1 之间。compression 可以控制结果的精确度,取值范围在 2014 - 10000 之间。值越大,精度越高,到内存开销和耗时越大。默认为 2048。
  • 返回类型:double类型

Example

mysql> select percentile_approx(query_time, 0.95), percentile_approx(query_time, 0.99) from tbl limit 10;
 +---------------------------------------+---------------------------------------+ |
 percentile_approx(`query_time`, 0.95) | percentile_approx(`query_time`, 0.99) | +----------
-----------------------------+---------------------------------------+ | 30.994913101196289
 | 116.05957794189453 | +---------------------------------------+-------------------------
--------------+ mysql> select `table`, percentile_approx(cost_time,0.99) from log_statis group by
 `table`; +---------------------+---------------------------+ | table | percentile_approx
(`cost_time`, 0.99) | +----------+--------------------------------------+ | test | 54.22 | +-----
-----+--------------------------------------+ mysql> select `table`, percentile_approx(cost_time,
0.99, 4096) from log_statis group by `table`; +---------------------+---------------------------
--------+ | table | percentile_approx(`cost_time`, 0.99, 4096.0) | +----------+------------------
----------------------------+ | test | 54.21 | +----------+----------------------------------------------+

Keywords

percentile_approx, percentile

TOPN

Description

topn(expr, int top_num[, int space_expand_rate])
  • 功能:该topn函数使用Space-Saving算法计算expr中的top_num个频繁项,结果为频繁项及其出现次数,该结果为近似值。space_expand_rate 参数是可选项,该值用来设置 Space-Saving 算法中使用的 counter个数。space_expand_rate 的值越大,结果越准确,默认值为50。

    counter numbers = top_num * space_expand_rate
  • 返回类型:JSON 格式的字符串。

Example

mysql> select topn(time, 10) from tbl; +------------------------------
----------------------------------------------------------------------
----------------------+ | topn(`time`, 10) | +------------------------
-----------------------------------------------------------------------
---------------------------+ | {"0":7894391,"1":3887461,"2":3792601,"6":3344590,
"5":2394986,"7":1421491,"3":1046929,"29":982826,"30":674072,"4":640616} | +-----
---------------------------------------------------------------------------------
------------------------------------+ mysql> select topn(time, 10, 100) from tbl; 
+-------------------------------------------------------------------------------
-------------------------------------------+ | topn(`time`, 10, 100) | +-----------
------------------------------------------------------------------------------------
---------------------------+ | {"0":7894592,"1":3887551,"2":3792700,"6":3344590,"5"
:2394986,"7":1421492,"3":1046977,"29":982826,"30":674072,"4":640625} | +-----------
--------------------------------------------------------------------------------
-------------------------------+

Keywords

topn

相似文档
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部