In today’s article, we will discuss the AVG Function, which helps us calculate the average value of a set in PostgreSQL.
The syntax of the AVG() function is as follows:
1 | AVG(column) |
For example, if we want to find out the average of item sales prices in our depo table, we can use the following sentence.
First, let’s look at how many items are in the table and their prices.
We can access the desired information with the following command.
1 | select * from depo where material='pen'; |
We can average item sales with the help of the following command.
1 | select AVG(sales_price::numeric) from depo where material='pen'; |
The purpose of the “::numeric” expression is to see the colon as a complete character due to the $ character, and it gives an error when run without this command.
We convert it to numeric data type to avoid errors and to return a healthy result.
1 |