Introduction to PostgreSQL Average
The average function is a function that can work on multiple rows at the same time. The result of the average function is a single result. The main functioning is finding the average value of a particular numeric column. It is usually used with the GROUP BY clause to have these rows associated together by some criteria. They can be used with queries with aggregate functions that operate on selected rows present in the result set’s target.
Explanation: This function can be used to select and having clauses. It will find the average or arithmetic mean of all input values which are provided. This function’s arguments can be of the data type: smallint, int, bigint, real, double precision, numeric, or interval.
Result: The result for this function is numeric for any integer type of argument. It is double for any floating argument or else the same data type as the argument. These can also be grouped when the Having clause is used in the query.
How Average function works in PostgreSQL?
Let us have a look at how the average function works.
Let us consider a column that has the following values:
The PostgreSQL AVG() function will help us in getting the average of this column, Marks, which has different marks. If the teacher of this class wants to find these five students’ average marks, then it can be found out easily by using the AVG() function.
Here the function converts all marks and gives them to the average function. All values in the column are sent to the average function, and the output is a single value, the average of these columns.
AVG(MARKS) MARKS(23, 78, 87, 56, 45)
The output of the above AVG() function will hence be 57.8
At the back end, the AVG() function takes the addition of all these five numbers and then divides the total by the number of marks. To explain this, it does:
23 + 78 + 87 + 56 + 45 = 289
289 / 5 = 57.8
The output of the above AVG() function will hence be 57.8
Examples to Implement PostgreSQL Average
Let’s look at some PostgreSQL avg function examples and explore how to use the avg function :
It may happen that a company would like to know the average amount charged by a customer. The below query can hence help in getting this value.
SELECT AVG(CHARGED_AMOUNT) AS "Average Charged Amount" FROM APP_IPTV.VOD_PAID_TRX_EVENT_STG;
Explanation: The amount’s average value is found by making use of this query. The charged amount’s average can be found and can be used further.
There can be cases where there are duplicates present in your table for a particular column. You would like to get the average, but you want the average to be of distinct values present in the table. This can also be done in PostgreSql using the AVG() function and the DISTINCT clause in the query.
SELECT avg(DISTINCT CYCLE_CODE) AS "Average Cycle Code" FROM APP_IPTV.VOD_PAID_RECON WHERE TRX_TYPE = 'EVENT';
Explanation: There are many similar entries for the column CYCLE_CODE in the above query. It may be needed that we require that for a filter condition like in the above query, TRX_TYPE = ‘EVENT’, and there are multiple CYCLE CODEs for this filter. Hence, we use the DISTINCT clause, which helps get distinct values present in the CYCLE_CODE table. After finding the distinct, the AVG() function works on these values and gets the needed average value. Above is the output which you will get of the query with the DISTINCT clause.
After finding the distinct, let us see how to make use of AVG() with a formula or a mathematical calculation. The AVG() function can also be used along with any formula. The below code with help you understand this better.
SELECT avg(AMOUNT * 10) AS "Average Amount" FROM ODS_ABP.BL1_RC_RATES;
Explanation: As explained, this example helps us understand the use of the AVG() function along with a calculation. Here, we have a column called ‘amount’, and we need to find the average value of this column. Along with the average, there is also the need for multiplying this amount by 10. The above query does this job. It first finds the average and then multiplies it with 10. The output is the value of this calculation.
The AVG() function in PostgreSQL helps in grouping by the results of average as per some field. You can group the results and also find the average in the same query. The below code will help you understand this better.
SELECT CUSTOMER_KEY, avg(AMOUNT) AS "Average Amount" FROM ODS_ABP.BL1_RC_RATES GROUP BY CUSTOMER_KEY;
Explanation: The above query is finding all customer keys along with the corresponding average amounts for the customer key groups. The customer key 91 has an average of 5.77. This means multiple customer keys are having the value of 91, and their collective average is 5.77. The group by clause helps us get the values per the particular conditions. Also, as AVG() is an aggregate function, it has to be accompanied by a group by clause. To improvise this and get the average result only up to two decimal places, the ROUND() function can also be used along with AVG(). The below query helps us in rounding off the values of the AVG() function.
SELECT CUSTOMER_KEY, ROUND(avg(AMOUNT),2) AS "Average Amount" FROM ODS_ABP.BL1_RC_RATES GROUP BY CUSTOMER_KEY;
Explanation: The HAVING clause can help us further add more conditions to the existing query. If the user wants to consider only ‘CUSTOMER’ keys with a value greater than 50 for calculating the average, then the following query will fulfill this requirement.
SELECT CUSTOMER_KEY, ROUND(avg(AMOUNT),2) AS "Average Amount" FROM ODS_ABP.BL1_RC_RATES GROUP BY CUSTOMER_KEY Having CUSTOMER_KEY > 50;
Explanation: All customer keys greater than 50 are selected.
The AVG() function in PostgreSQL is a very useful aggregate function. You can use it in any field to find the average of multiple rows and produce a single output. We can also use other functionalities of PostgreSQL and enhance its functioning.
We hope that this EDUCBA information on “PostgreSQL Average” was beneficial to you. You can view EDUCBA’s recommended articles for more information.