聚合函数
更新时间:2022-09-13
聚合函数的行为是将多行的结果聚合成一行。
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
AVG
Description
                SQL
                
            
            1avg(numeric val)
            - 功能:该聚合函数返回集合中的平均数。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL值的行将被忽略。如果该表是空的或者AVG 的参数都是NULL,则该函数返回NULL。当查询指定使用 
GROUP BY从句时,则每个GROUP BY的值都会返回1条结果。 - 返回类型: double类型
 
Example
                SQL
                
            
            1mysql> select ss_ticket_number, avg(ss_sales_price) from store_sales group by ss_ticket_number;
2+------------------+-----------------------+
3| ss_ticket_number | avg(`ss_sales_price`) |
4+------------------+-----------------------+
5|            28818 |             41.041875 |
6+------------------+-----------------------+
            Keywords
                Plain Text
                
            
            1avg
            COUNT
Description
                SQL
                
            
            1count([distinct] col_name...)
            - 
功能:该聚合函数返回满足要求的行的数目,或者非NULL行的数目。
- COUNT(*) 会计算包含NULL 值的行。
 - COUNT(column_name)仅会计算非NULL值的行。
 - 用户可以同时使用COUNT函数和DISTINCT操作符,count(distinct col_name...)会先对数据去重,然后再计算多个列的组合出现的次数。
 
 - 返回类型:int类型
 
Example
                SQL
                
            
            1mysql> select count(distinct tiny_column, short_column) from small_table;
2+-----------------------------------------------+
3| count(DISTINCT `tiny_column`, `short_column`) |
4+-----------------------------------------------+
5|                                             2 |
6+-----------------------------------------------+
            Keywords
                Plain Text
                
            
            1count
            APPROX_COUNT_DISTINCT
Description
                SQL
                
            
            1approx_count_distinct(type col)
            - 功能:该聚合函数返回指定列的去重值。不同于 
count(distinct)方法,该函数使用 HyperLogLog 算法返回有误差的去重值,但效率比count(distinct)高。 - 返回类型:int类型
 
Example
                SQL
                
            
            1mysql> select approx_count_distinct(ss_ticket_number) from store_sales;
2+-------------------------------------------+
3| approx_count_distinct(`ss_ticket_number`) |
4+-------------------------------------------+
5|                                     46580 |
6+-------------------------------------------+
            Keywords
                Plain Text
                
            
            1approx_count_distinct
            MAX
Description
                SQL
                
            
            1max(type col)
            - 功能:该聚合函数返回集合中的最大值。该函数和min函数的功能相反。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL值的行将被忽略。如果该表是空的或者MAX的参数都是NULL,则该函数返回NULL。当查询指定使用 
GROUP BY从句时,则每个GROUP BY的值都会返回1条结果。 - 返回类型:和输入参数相同的类型。
 
Example
                SQL
                
            
            1mysql> select ss_ticket_number, max(ss_sales_price) from store_sales group by ss_ticket_number;
2+------------------+-----------------------+
3| ss_ticket_number | max(`ss_sales_price`) |
4+------------------+-----------------------+
5|            28818 |                136.42 |
6+------------------+-----------------------+
            Keywords
                Plain Text
                
            
            1max
            MIN
Description
                SQL
                
            
            1min(type col)
            - 功能:该聚合函数返回集合中的最小值。该函数和max函数的功能相反。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL 值的行将被忽略。如果该表是空的或者MIN 的参数都是NULL,则该函数返回NULL。当查询指定使用 
GROUP BY从句时,则每个GROUP BY的值都会返回1条结果。 - 返回类型:和输入参数相同的类型。
 
Example
                SQL
                
            
            1mysql> select ss_ticket_number, min(ss_sales_price) from store_sales group by ss_ticket_number;
2+------------------+-----------------------+
3| ss_ticket_number | min(`ss_sales_price`) |
4+------------------+-----------------------+
5|            28818 |                  0.91 |
6+------------------+-----------------------+
            Keywords
                Plain Text
                
            
            1min
            SUM
Description
                SQL
                
            
            1sum(numeric col)
            - 功能:该聚合函数返回集合中所有值的和。该函数只有1个参数,该参数可以是数字类型的列,返回值是数字的函数,或者计算结果是数字的表达式。包含NULL值的行将被忽略。如果该表是空的或者MIN的参数都是NULL,则该函数返回NULL。当查询指定使用
GROUP BY从句时,则每个GROUP BY` 的值都会返回1条结果。 - 返回类型:如果参数整型,则返回BIGINT,如果参数是浮点型则返回double类型
 
Example
                SQL
                
            
            1mysql> select ss_ticket_number, sum(ss_sales_price) from store_sales group by ss_ticket_number;
2+------------------+-----------------------+
3| ss_ticket_number | sum(`ss_sales_price`) |
4+------------------+-----------------------+
5|            28818 |            1971980.01 |
6+------------------+-----------------------+
            Keywords
                Plain Text
                
            
            1sum
            GROUP_CONCAT
Description
                SQL
                
            
            1group_concat(col[, separator])
            - 功能:该聚合函数会返回1个字符串,该字符串是集合中所有字符串连接起来形成的新字符串。如果用户指定分隔符,则分隔符用来连接两个相邻行的字符串。
 - 返回类型:string类型
 - 使用说明:默认情况下,该函数返回1个覆盖所有结果集的字符串。当查询指定使用group by 从句时,则每个group by的值都会返回1条结果。
 
Example
                SQL
                
            
            1mysql> select * from tbl;
2+------+------+
3| k1   | v1   |
4+------+------+
5|    1 | a    |
6|    1 | b    |
7|    1 | c    |
8+------+------+
9
10mysql> select k1, group_concat(v1) from tbl group by k1;
11+------+--------------------+
12| k1   | group_concat(`v1`) |
13+------+--------------------+
14|    1 | a, b, c            |
15+------+--------------------+
16
17mysql> select k1, group_concat(v1, '|') from tbl group by k1;
18+------+-------------------------+
19| k1   | group_concat(`v1`, '|') |
20+------+-------------------------+
21|    1 | a|b|c                   |
22+------+-------------------------+
            Keywords
                Plain Text
                
            
            1group_concat
            VARIANCE,VAR_SAMP,VARIANCE_SAMP
Description
                SQL
                
            
            1variance(numeric val)
2var_samp(numeric val)
3variance_samp(numeric val)
            - 功能:该类聚合函数返回一组数的样本方差。这是一个数学属性,它表示值与平均值之间的距离。它作用于数值类型。
variance和var_samp是variance_samp的别名。 - 返回类型:double类型
 
Example
                SQL
                
            
            1mysql> select ss_ticket_number, variance(ss_sales_price) from store_sales group by ss_ticket_number;
2+------------------+----------------------------+
3| ss_ticket_number | variance(`ss_sales_price`) |
4+------------------+----------------------------+
5|            28818 |             1378.408511209 |
6+------------------+----------------------------+
            Keywords
                Plain Text
                
            
            1variance, var_samp, variance_samp
            VAR_POP,VARIANCE_POP
Description
                SQL
                
            
            1var_pop(numeric val)
2variance_pop(numeric val)
            - 功能:该类聚合函数返回一组数的总体方差。这是一个数学属性,它表示值与平均值之间的距离。它作用于数值类型。
var_pop是variance_pop的别名。 - 返回类型:double类型
 
Example
                SQL
                
            
            1mysql> select ss_ticket_number, variance_pop(ss_sales_price) from store_sales group by ss_ticket_number;
2+------------------+--------------------------------+
3| ss_ticket_number | variance_pop(`ss_sales_price`) |
4+------------------+--------------------------------+
5|            28818 |                 1378.408511134 |
6+------------------+--------------------------------+
            Keywords
                Plain Text
                
            
            1var_pop, variance_pop
            PERCENTILE_APPROX
Description
                SQL
                
            
            1percentile_approx(numeric val, double percentile, double compression)
            - 功能:该类聚合函数采用 T-Digest 算法,返回一组数的指定分位值的近似值。分位值 
percentile需在 0-1 之间。compression可以控制结果的精确度,取值范围在 2014 - 10000 之间。值越大,精度越高,到内存开销和耗时越大。默认为 2048。 - 返回类型:double类型
 
Example
                SQL
                
            
            1mysql> select percentile_approx(query_time, 0.95), percentile_approx(query_time, 0.99) from tbl limit 10;
2+---------------------------------------+---------------------------------------+
3| percentile_approx(`query_time`, 0.95) | percentile_approx(`query_time`, 0.99) |
4+---------------------------------------+---------------------------------------+
5|                    30.994913101196289 |                    116.05957794189453 |
6+---------------------------------------+---------------------------------------+
7
8mysql> select `table`, percentile_approx(cost_time,0.99) from log_statis group by `table`;
9+---------------------+---------------------------+
10| table    | percentile_approx(`cost_time`, 0.99) |
11+----------+--------------------------------------+
12| test     |                                54.22 |
13+----------+--------------------------------------+
14
15mysql> select `table`, percentile_approx(cost_time,0.99, 4096) from log_statis group by `table`;
16+---------------------+-----------------------------------+
17| table    | percentile_approx(`cost_time`, 0.99, 4096.0) |
18+----------+----------------------------------------------+
19| test     |                                        54.21 |
20+----------+----------------------------------------------+
            Keywords
                Plain Text
                
            
            1percentile_approx, percentile
            TOPN
Description
                SQL
                
            
            1topn(expr, int top_num[, int space_expand_rate])
            - 
功能:该topn函数使用Space-Saving算法计算expr中的top_num个频繁项,结果为频繁项及其出现次数,该结果为近似值。
space_expand_rate参数是可选项,该值用来设置 Space-Saving 算法中使用的 counter个数。space_expand_rate的值越大,结果越准确,默认值为50。Plain Text1counter numbers = top_num * space_expand_rate - 返回类型:JSON 格式的字符串。
 
Example
                SQL
                
            
            1mysql> select topn(time, 10) from tbl;
2+--------------------------------------------------------------------------------------------------------------------------+
3| topn(`time`, 10)                                                                                                         |
4+--------------------------------------------------------------------------------------------------------------------------+
5| {"0":7894391,"1":3887461,"2":3792601,"6":3344590,"5":2394986,"7":1421491,"3":1046929,"29":982826,"30":674072,"4":640616} |
6+--------------------------------------------------------------------------------------------------------------------------+
7
8mysql> select topn(time, 10, 100) from tbl;
9+--------------------------------------------------------------------------------------------------------------------------+
10| topn(`time`, 10, 100)                                                                                                    |
11+--------------------------------------------------------------------------------------------------------------------------+
12| {"0":7894592,"1":3887551,"2":3792700,"6":3344590,"5":2394986,"7":1421492,"3":1046977,"29":982826,"30":674072,"4":640625} |
13+--------------------------------------------------------------------------------------------------------------------------+
            Keywords
                Plain Text
                
            
            1topn
            