1

I have a table SoldCars.

Id_Car                               Id_Color                         time
8                                      2                          2015-02-11
8                                      4                          2015-03-11
8                                      2                          2015-04-11
5                                      2                          2015-05-11
8                                      3                          2015-08-11

I would like to query to get the following rowset:

Id_car          CountColors
8                   3
5                   1

Where Id_car is unique Id_Car and CountColors is count of distinct quantity of colors.

I've tried a lot of SQL queries, but the result is far from good.

For Example:

Select dictinct Id_Car, count(Id_Color) as CountColors from SoldCars group by Id_car
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
StepUp
  • 36,391
  • 15
  • 88
  • 148

1 Answers1

6
SELECT Id_car, count(DISTINCT Id_Color) AS CountColors FROM SoldCars GROUP BY Id_car

This should do the job :)

Tobias Baumeister
  • 2,107
  • 3
  • 21
  • 36