如何从下表中获取具有车辆数量的类别列表(按车辆数量的降序排列)。
车辆 | 类别
ab A
cd A
ef A
gh B
ij B
kl C
预期输出:
A 3
B 2
C 1
您可以将聚合函数count()
与 GROUP BY 一起使用:
select category, count(*) Total
from yourtable
group by category
order by total desc;
看演示
简单GROUP BY
,并检索计数。那是,
SELECT COUNT(*) AS repetitions FROM Vehicles GROUP BY category