I have a table with some rows, here is the table and row construction queries:
create table class_room(
section varchar(20),
section_name varchar(20),
section_id varchar(20),
student_rollno varchar(20)
);
insert into class_room values ('A','DIV A','01','547');
insert into class_room values ('A','DIV A','01','548');
insert into class_room values ('A','DIV A','01','549');
insert into class_room values ('A','DIV A','01','550');
insert into class_room values ('A','DIV A','01','551');
insert into class_room values ('A','DIV A','01','552');
insert into class_room values ('A','DIV A','01','567');
insert into class_room values ('A','DIV A','01','568');
insert into class_room values ('A','DIV A','01','593');
insert into class_room values ('A','DIV A','01','594');
insert into class_room values ('A','DIV A','01','595');
insert into class_room values ('A','DIV A','01','596');
insert into class_room values ('A','DIV A','01','597');
insert into class_room values ('A','DIV A','01','598');
Here user wants to see the ranges of student_rollno for certain section.
So I have tried the below code:
select section,section_name,section_id,
concat_ws('-',min(student_rollno),max(student_rollno)) as rollno_range
from class_room
where section='A'
group by section_id
having count(*)>=1;
The above code give me this result:
+---------+--------------+------------+--------------+
| section | section_name | section_id | rollno_range |
+---------+--------------+------------+--------------+
| A | DIV A | 01 | 547-598 |
+---------+--------------+------------+--------------+
But user wants to see the discrete values, which will looks like below solution:
+---------+--------------+------------+--------------------------------+
| section | section_name | section_id | rollno_range |
+---------+--------------+------------+--------------------------------+
| A | DIV A | 01 | 547-552, 567-568, 593-598 |
+---------+--------------+------------+--------------------------------+
I couldn't understand how to get output in above mentioned fashion. Please help.