Is it possible to use greater than equal to operator in group by? [closed]

If you are talking about using the > operator WITH the GROUP BY clause, then:

select
case when age between 0 and 20 then '0 - 20'
     when age between 21 and 40 then '21 - 40'
else '> 40' end age,
sum(case when survived = 1 then else 0 end) survivors
from rms_titanic
group by 
case when age between 0 and 20 then '0 - 20'
     when age between 21 and 40 then '21 - 40'
else '> 40'
order by
case when age between 0 and 20 then '0 - 20'
     when age between 21 and 40 then '21 - 40'
else '> 40';

This can be written as:

select
case when age between 0 and 20 then '0 - 20'
     when age between 21 and 40 then '21 - 40'
else '> 40' end age,
sum(case when survived = 1 then else 0 end) survivors
from rms_titanic
group by 1
order by 1;

However, if you want to use > operator on the result set of the GROUP BY, then you will have to including HAVING clause:

select
case when age between 0 and 20 then '0 - 20'
     when age between 21 and 40 then '21 - 40'
else '> 40' end age,
passenger_class,
sum(case when survived = 1 then else 0 end) survivors
from rms_titanic
group by 1, passenger_class having count(*) > 20
order by 1;

Browse More Popular Posts

Leave a Comment