morandi3
morandi3

Reputation: 1115

SQL group by - merge groups

Is it possible to "merge" 2 groups obtained after a SQL statement that use group by. For example if I have a field size ENUM('extra-small, 'small', 'medium', 'large', extra-large') and then I run this query SELECT * from clothes GROUP BY size;, but for in one case I would like to get in result "extra-small" and "small" in the same group. Is this possible with SQL?

Upvotes: 3

Views: 2352

Answers (2)

RomanKonz
RomanKonz

Reputation: 1027

yes, you can:

select count(*) 
    , case size 
        when 'extra-large' 
        then 'large' 
    else size end as grouped_size
from sizes
group by grouped_size

demo: http://sqlfiddle.com/#!2/ae3fa/2

Upvotes: 6

Adriaan Stander
Adriaan Stander

Reputation: 166396

How about using MySQL GROUP_CONCAT(expr)

This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.

Also have a look at MySQL – The GROUP_CONCAT() function

Upvotes: 0

Related Questions