-
Notifications
You must be signed in to change notification settings - Fork 302
GROUP BY Syntax en
谷深 edited this page Dec 16, 2022
·
1 revision
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.
select:
SELECT [ ALL | DISTINCT ]
{ * | projectItem [, projectItem ]* }
FROM tableExpression
[ WHERE booleanExpression ]
[ GROUP BY { groupItem [, groupItem ]* } ]
[ HAVING booleanExpression ]
The following table describes built-in user-defined aggregate functions (UDAF).
| No. | Function name | Feature |
-- | -- | --|
| 1 | SUM | Obtains the sum after aggregation. |
| 2 | MIN | Obtains the minimum value after aggregation. |
| 3 | MIN | Obtains the maximum value after aggregation. |
| 4 | COUNT | Obtains the number of data records. |
| 5 | AVG | Obtains the average value after aggregation. |
| 6 | ARBITRARY | Selects any input value as the output result. |
- sum
SELECT brand, SUM(price) FROM phone WHERE nid < 8 GROUP BY brand
- min
SELECT brand, MIN(price) FROM phone WHERE nid < 8 GROUP BY brand
- max
SELECT brand, MAX(price) FROM phone WHERE nid < 8 GROUP BY brand
- count
SELECT brand, COUNT(*) FROM phone WHERE nid < 8 GROUP BY brand
- avg
SELECT brand, AVG(price) FROM phone WHERE nid < 8 GROUP BY brand
- HAVING
SELECT brand FROM phone GROUP BY brand HAVING COUNT(brand) > 10