user1282609
user1282609

Reputation: 575

Count of result of a sql query based on values

The result of a temp table in sql is like:

ServiceType
-----------
E                             
I                             
I                             
E                             
I                             
D                             
I 
D
E

I want the count of above result something like this (where I-Imp,E-Exp,D-Dom):

Type   Count
------------
Exp     3
Imp     4
Dom     2      

Can anyone please suggest me the best way to achieve this? I am trying to do this by creating an other temp table.

Upvotes: 0

Views: 88

Answers (2)

Himanshu
Himanshu

Reputation: 32602

You can try this:

SELECT CASE ServiceType 
       WHEN 'I' THEN 'Imp' 
       WHEN 'E' THEN 'Exp' 
       WHEN 'D' THEN 'Dom' END AS Type
, COUNT(ServiceType) AS `Count`
FROM MyTable GROUP BY ServiceType

Result:

| TYPE | COUNT |
----------------
|  Dom |     2 |
|  Exp |     3 |
|  Imp |     4 |

See this SQLFiddle

Upvotes: 5

Shahid Iqbal
Shahid Iqbal

Reputation: 2135

try this..

select type, count(type) from table
group by type.

Upvotes: 1

Related Questions