1

I'm trying to use a SELECT DISTINCT to find out how many people have cars, how many have boats, now many bikes and so on, from this table

|   name   |   obj  |
---------------------
|  john    |  car   |
|  mary    |  boat  |
|  dave    |  car   |
|  james   |  bike  |
|  steve   |  car   |
|  walt    |  bike  |

ex: bike - 2

1
  • select distinct works on a row-basis. it's not filtering for distinct field contents. you need a select obj, count(obj) ... group by obj instead. Commented Sep 5, 2014 at 21:56

2 Answers 2

1

You can use this query

SELECT obj, COUNT(name) as nb FROM table GROUP BY obj ORDER BY obj

Result (sqlfiddle):

|   obj    |   nb   |
---------------------
|  bike    |    2   |
|  boat    |    1   |
|  car     |    3   |
Sign up to request clarification or add additional context in comments.

Comments

0
select sum(obj = 'car') as car_count,
       sum(obj = 'boat') as boat_count
from your_table

1 Comment

Could work, but what if i have 100 types of objects? In my case I have 23 different objects. Thanks for the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.