GManz
GManz

Reputation: 1659

select count of field by group

I have a table with the following table:

id | score | type
 1 |     1 |  "a"
 2 |     4 |  "b"
 3 |     2 |  "b"
 4 |    53 |  "c"
 5 |     8 |  "a"

I need to select the count of score based on the type. So my results will be:

totalScore | type
         9 |  "a"
         6 |  "b"
        53 |  "c"

I tried both DISTINCT and GROUP BY by neither seemed to work. The SQLs I tried:

SELECT * , COUNT(  `score` ) AS totalScore
FROM  `table`
GROUP BY  `type`

and

SELECT DISTINCT `type`, COUNT(score) as totalScore FROM `table`

But these don't seem to work.

Can anyone help?

Hosh

Upvotes: 1

Views: 207

Answers (2)

user189688
user189688

Reputation:

select sum(score),type from table group by type

Upvotes: 3

JamesHalsall
JamesHalsall

Reputation: 13485

This should work...

SELECT sum(score) AS total FROM table GROUP BY type

Upvotes: 3

Related Questions