Overview
GROUP BY is used to aggregate one or more fields to calculate the metrics for the required data, such as the maximum value, minimum value, and average value.
Supported versions
OpenSearch Retrieval Engine Edition whose HA3 version is V3.7.0 or later
Syntax
select:
SELECT [ ALL | DISTINCT ]
{ * | projectItem [, projectItem ]* }
FROM tableExpression
[ WHERE booleanExpression ]
[ GROUP BY { groupItem [, groupItem ]* } ]
[ HAVING booleanExpression ]Built-in UDAFs
The following table describes built-in user-defined aggregate functions (UDAFs).
No. | Function | Feature |
1 | SUM | Returns the sum of aggregated values. |
2 | MIN | Returns the minimum value of aggregated values. |
3 | MAX | Returns the maximum value of aggregated values. |
4 | COUNT | Calculates the number of records. |
5 | AVG | Returns the average value of aggregated values. |
6 | ARBITRARY | Returns an input value as the output result. |
You can customize a UDAF to extend the logic that is used to aggregate data. For more information, see UDAF overview.
Examples
SUM
SELECT brand, SUM(price) FROM phone WHERE nid < 8 GROUP BY brandMIN
SELECT brand, MIN(price) FROM phone WHERE nid < 8 GROUP BY brandMAX
SELECT brand, MAX(price) FROM phone WHERE nid < 8 GROUP BY brandCOUNT
SELECT brand, COUNT(*) FROM phone WHERE nid < 8 GROUP BY brandAVG
SELECT brand, AVG(price) FROM phone WHERE nid < 8 GROUP BY brandHAVING clause
SELECT brand FROM phone GROUP BY brand HAVING COUNT(brand) > 10