Overview
TheAVG()
function lets you calculate the average value of records. The input and return types we support can be seen in the table below:
Input type | Return type |
---|---|
INTEGER | DOUBLE PRECISION |
BIGINT | DOUBLE PRECISION |
REAL | DOUBLE PRECISION |
DOUBLE PRECISION | DOUBLE PRECISION |
If the input type is 32-bit, then the result will be 64-bit
Examples
In this example, we will use an orders table that stores details of the purchase transactions:AVG() with a single expression
In the first example, we want to calculate the average amount of all orders that customers have paid:AVG() with a GROUP BY clause
The following example uses theAVG()
function and GROUP BY
clause to calculate the average amount paid by each customer:
-
First, the
GROUP BY
clause divides orders into groups based on customers -
Then, the
AVG
function is applied to each group.
::NUMERIC(10,2)
to add two decimal numbers after the comma: