Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 338
morandi3 Avatar asked Mar 13 '26 19:03

morandi3


1 Answers

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

like image 134
RomanKonz Avatar answered Mar 15 '26 07:03

RomanKonz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!