PostgreSQL aggregate functions

Table of contents

overview

SUM()

AVG()

COUNT()

MAX() 

MIN()

ARRAY_AGG()


overview

There is another type of SQL function, called aggregation (or aggregation, grouping) function, which is a function that summarizes a set of data. The input is a set of data, and the output is a single value.

Illustration:

insert image description here

SUM()

Meaning: Returns the total number of numeric columns.

适用:PostGreSql、MySQL、ORACLE 、Microsoft SQL Server

grammar:

SELECT AVG(column_name) FROM table_name

AVG()

Returns the average of a numeric column.

适用:PostGreSql、 ORACLE 、Microsoft SQL Server、MySQL

grammar:

SELECT AVG(column_name) FROM table_name

COUNT()

Meaning: Returns the number of rows matching the specified criteria.

适用:PostGreSql、 ORACLE 、Microsoft SQL Server、MySQL

Notice:

  • COUNT(*) returns the total number of records in the table, applicable to any data type .
  • COUNT(expr) returns the total number of records where expr is not empty .
  • COUNT(*) counts rows with NULL values, but COUNT(column name) does not count rows with NULL values ​​in this column.

grammar:

--COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入):
SELECT COUNT(column_name) FROM table_name;

--COUNT(*) 函数返回表中的记录数(无法用于 Microsoft Access):
SELECT COUNT(DISTINCT column_name) FROM table_name;

--COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目:
SELECT COUNT(*) FROM table_name;

MAX() 

Meaning: returns the maximum value of the specified column

适用:PostGreSql、 ORACLE 、Microsoft SQL Server、MySQL

grammar:

SELECT MAX(column_name) FROM table_name;

MIN()

Meaning: Returns the minimum value of the specified column.

适用:PostGreSql、 ORACLE 、Microsoft SQL Server、MySQL

grammar:

SELECT MIN(column_name) FROM table_name;

ARRAY_AGG()

Meaning: Returns an array consisting of all values ​​in the field within the group.

Applicable: PostGreSql,

grammar:

SELECT ARRAY_AGG(column_name) FROM table_name;

Guess you like

Origin blog.csdn.net/yeyaozhifengqi/article/details/130348939