Skip to content

Grouping with GROUP BY · ⁨用 GROUP BY 分组⁩

English

Grouping rows with GROUP BY

An aggregate like AVG normally summarises the whole table into one number. GROUP BY splits the rows into groups that share a value, and gives one summary row per group:

This gives one row for each form, with that form's count and average.

中文

用 GROUP BY 分组

像 AVG 这样的聚合函数,通常把整张表汇总成一个数。GROUP BY 会把行分成共享同一个值的组,并为每个组给出一行汇总:

SELECT form, COUNT(*) AS n, ROUND(AVG(score), 2) AS avg_score
FROM student
GROUP BY form;

这样每个 form 得到一行,显示该 form 的人数和平均分。

GROUP BY 把同值的行合并成每组一行,并对其余列求和

Handout

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩