5

我有一个超过 30K 行和多列的表。

例子:

id | year | make | model | color

1  | 2001 | gm     | truck  | red
2  | 2004 | gm     | truck  | green
3  | 2001 | nissan | Max    | yellow
4  | 2001 | gm     | truck  | blue
5  | 2002 | gm     | truck  | green
6  | 2001 | nissan | Sentra | green

由于每个品牌型号和年份都有很多颜色,我需要找出每辆车有多少颜色。

期望的结果:

2001 Nissan Max 5 colors
2001 GM Truck 10 colors

不需要知道有多少颜色是什么颜色。

我尝试了以下方法:

SELECT COUNT(DISTINCT make||model||year) AS number FROM colors LIMIT 10

任何帮助将非常感激

4

1 回答 1

5

你几乎拥有它:

SELECT make,
       model,
       year,
       COUNT(DISTINCT color) AS number 
FROM colors
GROUP BY make, model, year
LIMIT 10;
于 2013-07-25T08:10:37.643 回答