Usage of groupby in sqlserver

Source: Internet
Author: User
Welcome to the Windows community forum and interact with 3 million technical staff. Each predicate compares the group features (such as AVG (SALARY) with one of the following features: HAVINGAVG (SALARY) 2 * MIN (SALARY) constant for example: HAVINGAVG (SALARY) 20000 for example, the following query finds the number of employees that exceed

Welcome to the Windows community forum, and interact with 3 million technical staff> enter each predicate to compare the group features (such as AVG (SALARY) with one of the following, for example: having avg (SALARY) 2 * MIN (SALARY) constant for example: having avg (SALARY) 20000 for example, the following query finds the number of employees exceeds

Welcome to the Windows community forum and interact with 3 million technicians>

Each predicate compares a group feature (for example, AVG (SALARY) with one of the following, for example, having avg (SALARY)> 2 * MIN (SALARY) constants.

For example: having avg (SALARY)> 20000

For example, the following query queries the highest and lowest salaries of departments with more than 4 employees:

Code example: select workdept, MAX (SALARY) as maximum, MIN (SALARY) AS MINIMUM

FROM EMPLOYEE

GROUP BY WORKDEPT

Having count (*)> 4 ORDER BY WORKDEPT

Result:

WORKDEPT MAXIMUM MINIMUM

------------------------------

D11 32250.00 18270.00

D21 36170.00 17250.00

E11 29750.00 15340.00

It is possible (though rare) that a query has a HAVING clause but does not have a group by clause. In this case, DB2 regards the entire table as a group. Because the table is regarded as a single group, there can be a maximum of one result row. If the HAVING condition is true for the entire table, the selected result is returned (the result must be composed of column functions); otherwise, no rows are returned. Officially speaking, group-based operations, or better understanding, are class-based operations.

Use the following example to deepen your understanding.

The table is as follows:

Code example: ID name warehouse quantity

01 watermelon 1 10

02 watermelon 2 30

03 rice 1 30

04 Apple 1 40

05 Apple 2 45

06 apple 3 5

Statement:

Code example: Select name, Sun (price) From Table Group By name

Returns the total number of items, no matter which warehouse

Result:

Watermelon, 40

Rice, 30

Apple, 90

[1] [2]

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.