AVG

2024-03-05 01:54:27  Updated

Syntax

AVG([DISTINCT | ALL] expr)

Purpose

AVG() returns the average value of a specified group. NULLs are ignored. The DISTINCT option can be used to return the average of distinct values of expr. If no matching row is found, AVG() returns NULL.

Examples

obclient> SELECT * FROM oceanbasetest;
+----+------+------+
| id | ip   | ip2  |
+----+------+------+
|  1 |    4 | NULL |
|  3 |    3 | NULL |
|  4 |    3 | NULL |
+----+------+------+
3 rows in set

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

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

Contact Us