提问者:小点点

如何获取特殊计数


跟踪是否可以得到特殊的计数?

ItemId | Colors
10     |  red
10     |  red
10     |  blue
20     |  red
20     |  blue
20     |  green

我需要

Colors | Count
red    | 2  (not 3)
blue   | 2
green  | 1

如果同一ItemId的颜色出现多次,则只计算一次


共2个答案

匿名用户

SELECT Colors, COUNT(DISTINCT ItemId, Colors) `Count`
FROM test
GROUP BY Colors;

小提琴

匿名用户

该查询将给出所需的输出:

select count(distinct itemid),color from item group by color;