Salis
Salis

Reputation: 443

SQL: Select sum of each individual value of a column

There is a column that can have several values. I want to select a count of how many times each distinct value occurs in the entire set. I feel like there's probably an obvious solution but it's eluding me. I'll show input & expected output, hopefully it's obvious enough that you'll understand.

This is example data:

|-------|
| class |
|-------|
| 5     |
| 5     |
| 12    |
| 4     |
| 12    |
|-------|

This is the output I'm trying to get:

|-------|-------|
| class | count |
|-------|-------|
| 5     | 2     |
| 12    | 2     |
| 4     | 1     |
|-------|-------|

Upvotes: 0

Views: 992

Answers (4)

Er. Anurag Jain
Er. Anurag Jain

Reputation: 1793

Please try this

SELECT class , count( class ) AS COUNT FROM `tble` GROUP BY class

Upvotes: 0

Shaikh Farooque
Shaikh Farooque

Reputation: 2640

Make Count as column

select class, count(1) as Count
from table
group by class

Upvotes: 0

duncanportelli
duncanportelli

Reputation: 3229

SELECT CLASS, COUNT (*)
FROM MYTABLE
GROUP BY CLASS

Upvotes: 5

hkutluay
hkutluay

Reputation: 6944

select class, count(1)
from table
group by class

Upvotes: 1

Related Questions