user3160441
user3160441

Reputation: 163

sum of few rows within group by

I need to aggregate few rows, but unfortunately not all.

What I currently have:

select type,count(*) from world group by type;

Type   | count(*)
---------------
Man    |   10
Woman  |   20
Dog    |   2
Cat    |   1
Turtle |   2

And I need to format this query to obtain such table

Type    | count(*)
---------------
Man     |   10
Woman   |   20
Animals |   5

Is it doable by one query?

Upvotes: 0

Views: 52

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

You can do :

select (case when type not in ('man', 'women') 
             then 'Animals' 
             else type 
         end), count(*)
from world 
group by (case when type not in ('man', 'women') 
               then 'Animals' 
               else type 
           end);

Upvotes: 4

Related Questions