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

成都創(chuàng)新互聯(lián)主營(yíng)榆林網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App制作,榆林h5微信小程序開發(fā)搭建,榆林網(wǎng)站營(yíng)銷推廣歡迎榆林等地區(qū)企業(yè)咨詢
所有聚合函數(shù)都具有確定性。任何時(shí)候用一組給定的輸入值調(diào)用它們時(shí),都返回相同的值。
在 OceanBase 數(shù)據(jù)庫(kù)的聚合函數(shù)中,Value 表達(dá)式只能出現(xiàn)一個(gè)。例如:不支持 ?COUNT(c1, c2)?,僅支持?COUNT(c1)?。
AVG
聲明
?AVG(([DISTINCT] expr)?
說(shuō)明
返回指定組中的平均值,空值被忽略。?DISTINCT? 選項(xiàng)可用于返回 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)?
說(shuō)明
該函數(shù)返回 ?SELECT? 語(yǔ)句檢索到的行中非 NULL 值的數(shù)目。若找不到匹配的行,則 ?COUNT()? 返回 0。?DISTINCT? 選項(xiàng)可用于返回 ?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)?
說(shuō)明
返回指定數(shù)據(jù)中的最大值。
?MAX()? 的取值可以是一個(gè)字符串參數(shù),在這些情況下,它們返回最大字符串值。?DISTINCT? 關(guān)鍵字可以被用來(lái)查找 ?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)?
說(shuō)明
返回指定數(shù)據(jù)中的最小值。
?MIN()? 的取值可以是一個(gè)字符串參數(shù),在這些情況下,它們返回最小字符串值。?DISTINCT? 關(guān)鍵字可以被用來(lái)查找? 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)?
說(shuō)明
返回 expr 的總數(shù)。若返回集合中無(wú)任何行,則 ?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)?
說(shuō)明
該函數(shù)返回帶有來(lái)自一個(gè)組的連接的非 NULL 值的字符串結(jié)果。
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
ASC | DESC]
[SEPARATOR str_val])
例子
obclient> select * from book; //表book(書編號(hào),書名,出版社)
+--------+--------------------------------+-----------------------------+
| bookid | bookname | publishname |
+--------+--------------------------------+-----------------------------+
| 1 | git help | alibaba group publisher |
| 2 | MySQL性能優(yōu)化 | 浙江大學(xué)圖文出版社 |
| 3 | JAVA編程指南 | 機(jī)械工業(yè)出版社 |
| 3 | JAVA編程指南 | 機(jī)械工業(yè)出版社 |
| 4 | 大規(guī)模分布式存儲(chǔ)系統(tǒng) | 機(jī)械工業(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ī)模分布式存儲(chǔ)系統(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ī)模分布式存儲(chǔ)系統(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編程指南 | 機(jī)械工業(yè)出版社;機(jī)械工業(yè)出版社 |
| MySQL性能優(yōu)化 | 浙江大學(xué)圖文出版社 |
| 大規(guī)模分布式存儲(chǔ)系統(tǒng) | 機(jī)械工業(yè)出版社 |
+--------------------------------+---------------------------------------------------------------------+
4 rows in set (0.00 sec)
本文標(biāo)題:創(chuàng)新互聯(lián)OceanBase教程:OceanBase聚合函數(shù)
當(dāng)前網(wǎng)址:http://www.5511xx.com/article/dhcspjc.html


咨詢
建站咨詢
