我有一个看起来像这样的表:

Pizza  ----|| Topping
====================
Cheese--||    Olives
====================
Ham-----     || Tuna
====================
Cheese-- || Olives
====================
Cheese-- || Tuna
====================


我想获得每种类型的比萨的数量,因此在上表中将有一个火腿比萨和2个奶酪比萨(因为重复了奶酪+橄榄,并且应该只计算一次)。基本上,我需要为每两个相等的元素计数每一行,然后按披萨分组。

该查询的结果应为:

Pizza  ----|| Count
====================
Cheese--||    2
====================
Ham-----     || 1
====================


谢谢!

最佳答案

select Pizza, count( distinct Topping) as cnt
from your_pizza_table
group by Pizza
order by cnt desc;

10-08 03:59