PostgreSQL avg () function

Source: Internet
Author: User

PostgreSQL avg () function

The AVG function of PostgreSQL is used to find the average value of a field in various records.

To understand the AVG function, the table COMPANY has the following records:
Testdb # select * from COMPANY;
Id | name | age | address | salary
---- + ------- + ----- + ----------- + --------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | TX | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall | 45000
7 | James | 24 | houaston | 10000
(7 rows)
 

Now, if you want to calculate the average salary based on the above table, you can use the following command: www.bkjia.com
Testdb = # select avg (SALARY) from company;

The above PostgreSQL table will produce the following results:
Avg
------------------
37142.8571428571
(1 row)
 

The average group by clause can be used to set various records. In the following example, all related records of a person are averaged, and the average salary of each person is calculated.
Testdb = # SELECT name, AVG (SALARY) from company group by name;
Name | avg
------- + -------
Teddy | 20000
Paul | 20000
Mark | 65000
David | 85000
Allen | 15000
Kim | 45000
James | 10000
(7 rows)

That which didn't kill me makes me stronger

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.