Column-Based GROUP BY
Function
This statement is used to group a table based on columns.
Syntax
1 2 | SELECT attr_expr_list FROM table_reference
GROUP BY col_name_list;
|
Keyword
Column-based GROUP BY can be categorized into single-column GROUP BY and multi-column GROUP BY.
- Single-column GROUP BY indicates that the GROUP BY clause contains only one column. The fields in attr_expr_list must exist in col_name_list. The aggregate function, count() and sum() for example, is supported in attr_expr_list. The aggregate function can contain other fields.
- Multi-column GROUP BY indicates that there is more than one column in the GROUP BY clause. The query statement is grouped according to all the fields in the GROUP BY clause. The records with the same fields are put in the same group. Similarly, the fields in the attr_expr_list must be in the fields in the GROUP BY clause. The attr_expr_list field can also use the aggregate function.
Precautions
The to-be-grouped table must exist. Otherwise, an error is reported.
Example
Group the student table according to the score and name fields and return the grouping results.
1 2 | SELECT score, count(name) FROM student
GROUP BY score,name;
|
Last Article: Grouping
Next Article: Expression-Based GROUP BY
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.