OceanBase 聚合函數(shù)

2021-06-09 15:45 更新

聚合函數(shù)對一組值執(zhí)行計算并返回單一的值。聚合函數(shù)忽略空值。聚合函數(shù)經(jīng)常與?SELECT語句的 GROUP BY 子句一同使用。

所有聚合函數(shù)都具有確定性。任何時候用一組給定的輸入值調(diào)用它們時,都返回相同的值。

在 OceanBase 數(shù)據(jù)庫的聚合函數(shù)中,Value 表達式只能出現(xiàn)一個。例如:不支持 ?COUNT(c1, c2)?,僅支持?COUNT(c1)?。

AVG

聲明

?AVG(([DISTINCT] expr)?

說明

返回指定組中的平均值,空值被忽略。?DISTINCT? 選項可用于返回 expr 的不同值的平均值。若找不到匹配的行,則 ?AVG()? 返回 NULL。

例子

obclient> select * from oceanbasetest;
+----+------+------+
| id | ip   | ip2  |
+----+------+------+
|  1 |    4 | NULL |
|  3 |    3 | NULL |
|  4 |    3 | NULL |
+----+------+------+
3 rows in set (0.01 sec)

obclient> select avg(ip2), avg(ip), avg(distinct(ip)) from oceanbasetest;
+----------+---------+-------------------+
| avg(ip2) | avg(ip) | avg(distinct(ip)) |
+----------+---------+-------------------+
|     NULL |  3.3333 |            3.5000 |
+----------+---------+-------------------+
1 row in set (0.00 sec)

obclient> select avg(distinct(ip)),avg(ip),avg(ip2) from oceanbasetest;
+-------------------+---------+----------+
| avg(distinct(ip)) | avg(ip) | avg(ip2) |
+-------------------+---------+----------+
|            3.5000 |  3.3333 |     NULL |
+-------------------+---------+----------+
1 row in set (0.00 sec)

COUNT

聲明

?COUNT([DISTINCT] expr)?

說明

該函數(shù)返回 ?SELECT? 語句檢索到的行中非 NULL 值的數(shù)目。若找不到匹配的行,則 ?COUNT()? 返回 0。?DISTINCT? 選項可用于返回 ?expr? 的不同值的數(shù)目。

?COUNT(*)? 的稍微不同之處在于,它返回檢索行的數(shù)目,不論其是否包含 NULL 值。

例子

obclient> select * from oceanbasetest;
+----+------+------+
| id | ip   | ip2  |
+----+------+------+
|  1 |    4 | NULL |
|  3 |    3 | NULL |
|  4 |    3 | NULL |
+----+------+------+
3 rows in set (0.00 sec)

obclient> select count(ip2), count(ip), count(distinct(ip)), count(*) from oceanbasetest;
+------------+-----------+---------------------+----------+
| count(ip2) | count(ip) | count(distinct(ip)) | count(*) |
+------------+-----------+---------------------+----------+
|          0 |         3 |                   2 |        3 |
+------------+-----------+---------------------+----------+
1 row in set (0.00 sec)

MAX

聲明

?MAX([DISTINCT] expr)?

說明

返回指定數(shù)據(jù)中的最大值。

?MAX()? 的取值可以是一個字符串參數(shù),在這些情況下,它們返回最大字符串值。?DISTINCT? 關(guān)鍵字可以被用來查找 ?expr? 的不同值的最大值,這產(chǎn)生的結(jié)果與省略 ?DISTINCT? 的結(jié)果相同。

假設(shè)表 a 有三行數(shù)據(jù):id=1,num=10;id=2,num=20;id=3,num=30。

例子

obclient> SELECT MAX(num) FROM a;
+-----------------+
| MAX(num)        |
+-----------------+
|              30 |
+-----------------+
1 row in set (0.00 sec)

MIN

聲明

?MIN([DISTINCT] expr)?

說明

返回指定數(shù)據(jù)中的最小值。

?MIN()? 的取值可以是一個字符串參數(shù),在這些情況下,它們返回最小字符串值。?DISTINCT? 關(guān)鍵字可以被用來查找? expr ?的不同值的最小值,然而,這產(chǎn)生的結(jié)果與省略 ?DISTINCT ?的結(jié)果相同。

假設(shè)表 a 有三行數(shù)據(jù):id=1,num=10;id=2,num=20;id=3,num=30。

例子

obclient> SELECT MIN(num) FROM a;
+----------------+
| MIN(num)       |
+----------------+
|             10 |
+----------------+
1 row in set (0.00 sec)

SUM

聲明

?SUM([DISTINCT] expr)?

說明

返回 expr 的總數(shù)。若返回集合中無任何行,則 ?SUM() ?返回 NULL。?DISTINCT? 關(guān)鍵字可用于求得 ?expr? 不同值的總和。

若找不到匹配的行,則 ?SUM()? 返回 NULL。

例子

obclient> select * from oceanbasetest;
+------+------+------+
| id   | ip   | ip2  |
+------+------+------+
|    1 |    4 | NULL |
|    3 |    3 | NULL |
|    4 |    3 | NULL |
+------+------+------+
3 rows in set (0.00 sec)

obclient> select sum(ip2),sum(ip),sum(distinct(ip)) from oceanbasetest;
+----------+---------+-------------------+
| sum(ip2) | sum(ip) | sum(distinct(ip)) |
+----------+---------+-------------------+
|     NULL |      10 |                 7 |
+----------+---------+-------------------+
1 row in set (0.00 sec)

GROUP_CONCAT

聲明

?GROUP_CONCAT([DISTINCT] expr)?

說明

該函數(shù)返回帶有來自一個組的連接的非 NULL 值的字符串結(jié)果。

GROUP_CONCAT([DISTINCT] expr [,expr ...]
    [ORDER BY {unsigned_integer | col_name | expr}
     ASC | DESC]
    [SEPARATOR str_val])

例子

obclient> select * from book;     //表book(書編號,書名,出版社)
+--------+--------------------------------+-----------------------------+
| bookid | bookname                       | publishname                 |
+--------+--------------------------------+-----------------------------+
|      1 | git help                       | alibaba group publisher     |
|      2 | MySQL性能優(yōu)化                  | 浙江大學(xué)圖文出版社          |
|      3 | JAVA編程指南                   | 機械工業(yè)出版社              |
|      3 | JAVA編程指南                   | 機械工業(yè)出版社              |
|      4 | 大規(guī)模分布式存儲系統(tǒng)           | 機械工業(yè)出版社              |
+--------+--------------------------------+-----------------------------+
5 rows in set (0.00 sec)    

//查找書名信息
obclient> select group_concat(bookname) from book group by bookname;
+-----------------------------------+
| group_concat(bookname)            |
+-----------------------------------+
| git help                          |
| JAVA編程指南,JAVA編程指南         |
| MySQL性能優(yōu)化                     |
| 大規(guī)模分布式存儲系統(tǒng)              |
+-----------------------------------+
4 rows in set (0.00 sec)

//查找書名信息,書名唯一
obclient> select group_concat(distinct(bookname)) from book group by bookname;
+----------------------------------+
| group_concat(distinct(bookname)) |
+----------------------------------+
| git help                         |
| JAVA編程指南                     |
| MySQL性能優(yōu)化                    |
| 大規(guī)模分布式存儲系統(tǒng)             |
+----------------------------------+
4 rows in set (0.01 sec)    

//查找書名和出版社信息,以書名分組,出版社信息降序排序顯示
obclient> select bookname, group_concat(publishname order by publishname desc separator  ';' ) from book group by bookname;
+--------------------------------+---------------------------------------------------------------------+
| bookname                       | group_concat(publishname order by publishname desc separator  ';' ) |
+--------------------------------+---------------------------------------------------------------------+
| git help                       | alibaba group publisher                                             |
| JAVA編程指南                   | 機械工業(yè)出版社;機械工業(yè)出版社                                       |
| MySQL性能優(yōu)化                  | 浙江大學(xué)圖文出版社                                                  |
| 大規(guī)模分布式存儲系統(tǒng)           | 機械工業(yè)出版社                                                      |
+--------------------------------+---------------------------------------------------------------------+
4 rows in set (0.00 sec)


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號