跟踪是否可以得到特殊的计数?
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的颜色出现多次,则只计算一次
SELECT Colors, COUNT(DISTINCT ItemId, Colors) `Count`
FROM test
GROUP BY Colors;
小提琴
该查询将给出所需的输出:
select count(distinct itemid),color from item group by color;