Dark Horse programmer-the meaning and usage of the Group by statement in SQL

Source: Internet
Author: User

 

-------------------- Windows Phone 7 Mobile Phone development,. Net training, and hope to communicate with you! ----------------------

 

The book says that group by can divide a table into logical groups and calculate aggregate statistics for each group .. I didn't understand the meaning of this sentence at first, and I finally understood it with the help of everyone.

The literal meaning of group by is: grouping.

Assume that there is a t_person table.
The table has the following attributes: ID, name, age, and salary.
Then we need to retrieve the age groups (by age group) and the corresponding highest salary (the clustering function acts on each group ):
Select age as age, max (salary) as maximum salary from t_person group by age

For example:
ID name age salary
1 Steven 19 5362
2 WiFi 19 5620
3 Amy 21 5320
4 bin 23 5000
5 fin 22 5600
6 okn 23 6520

Output:
Maximum age wage

19 5620

21 5320

22 5600

23 6520

NOTE: If no query group is configured, the aggregate function applies to the entire query result! Only the maximum 6520

 

-------------------- Windows Phone 7 Mobile Phone development,. Net training, and hope to communicate with you! ---------------------- For details, please refer to: http://net.itheima.com/

 

-------------------- Windows Phone 7 Mobile Phone development,. Net training, and hope to communicate with you! ----------------------

 

The book says that group by can divide a table into logical groups and calculate aggregate statistics for each group .. I didn't understand the meaning of this sentence at first, and I finally understood it with the help of everyone.

The literal meaning of group by is: grouping.

Assume that there is a t_person table.
The table has the following attributes: ID, name, age, and salary.
Then we need to retrieve the age groups (by age group) and the corresponding highest salary (the clustering function acts on each group ):
Select age as age, max (salary) as maximum salary from t_person group by age

For example:
ID name age salary
1 Steven 19 5362
2 WiFi 19 5620
3 Amy 21 5320
4 bin 23 5000
5 fin 22 5600
6 okn 23 6520

Output:
Maximum age wage

19 5620

21 5320

22 5600

23 6520

NOTE: If no query group is configured, the aggregate function applies to the entire query result! Only the maximum 6520

 

-------------------- Windows Phone 7 Mobile Phone development,. Net training, and hope to communicate with you! ---------------------- For details, please refer to: http://net.itheima.com/

 

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.