R
R
Rphoenix2020-03-30 08:03:00
SQL
Rphoenix, 2020-03-30 08:03:00

Group eating?

Hello!
ID - class number (1-11), NAME - class name (first class - eleventh class, i.e. string) and COUNT - how many students are in the class. Let's say so.

select ID, NAME, COUNT(*) from class c
group by c.id, c.name

It outputs

1 first class 40
2 second class ....

And I need three lines

Elementary school
Middle school
Junior school

where sum is 1 -4, 5-9 and 10-11 respectively.
That is, case when .... having ... is needed here? What is more correct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-03-30
@Rphoenix

select case when id in (1,2,3,4) then 'Начальная школа'
            when id in (5,6,7,8,9) then 'Средняя школа'
            when id in (10,11) then 'Младшая школа'
           end Class_type,
 COUNT(*) from class c
group by Class_type

Just put the names for the classes correctly, otherwise 10.11 is clearly not 'Junior School' :-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question