Database (grouping function)

Source: Internet
Author: User
Grouping function: operate on a group of records in the Table. Only one result is returned each time. Common functions include MIN, MAX, SUM, AVG, COUNT, and other MIN and MAX return the minimum and maximum values of each group. Eg: selectmin (salary), max (salary) fromemployees; the SUM and AVG functions return the SUM mean respectively. Eg: selectsum (salary), avg (salary)

Grouping function: operate on a group of records in the Table. Only one result is returned each time. Common functions include MIN, MAX, SUM, AVG, COUNT, and other MIN and MAX return the minimum and maximum values of each group. Eg: select min (salary), max (salary) from employees; the SUM and AVG functions return the SUM mean respectively. Eg: select sum (salary), avg (salary)

Grouping function: operate on a group of records in the Table. Only one result is returned each time.

Common functions include MIN, MAX, SUM, AVG, and COUNT.


MIN and MAX return the minimum and maximum values of each group.

Eg: select min (salary), max (salary) from employees;


The SUM and AVG functions return the average SUM respectively.

Eg: select sum (salary), avg (salary) from emloyees;


The count function mainly returns the number of records in each group.

Eg: select count (*) from employees where job_id = '';


Use of group by... HAVING clause.

The group function ignores NULL values. You can use the NVL, NVL2, and COALESCE functions to process null values.

Eg: average salary of employees with the same manager in the same position.

Select job_id, manager_id, avg (salary)

From employees

Group by job_id, manager_id


* Notes about the group by clause:

1. Columns after the group by clause can no longer appear in the SELECT statement.

2. The "non-grouping function" in the SELECT clause must appear in the group by clause.


HAVING clause.

Error example:

Select job_id, max (salary)

From employees

Where max (salary)> = 8888

Group by job_id;

The reason is that the execution order of the query statement is: from where group by select order


Correct example:

Select job_id, max (salary)

From employees

Group by job_id

Having max (salary) & gt; 8888;

Summary:
-Find the table to be queried using the FROM clause;
-Use the WHERE clause to filter and judge non-grouping functions;
-GROUP operations are performed using the group by clause;
-Use the HAVING clause to filter and judge group functions;
-Use the SELECT clause to SELECT the displayed columns, expressions, and group functions;
-Sort BY clauses.


Eg:

Select department_id, max (salary)

From employees

Where department_id between 30 and 90

Group by department_id

Having max (salary) & gt; 8888

Order by max (salary );








Zookeeper

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.