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]