Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table with the following structure and with the data

Class Studentid

1 1
1 2
1 3
1 4
1 5

2 1
2 2
2 3

3 1
3 2
3 3
3 4

4 1

5 1
5 2

I need to take the result The the lowest student id and the highest student id as follow as
Class Studentid

1 1
1 5

2 1
2 3

3 1
3 4

4 1

5 1
5 2
Posted

1 solution

Hi Sacraj,

I have created a schema and you could use the below query to get the records
SQL
create table sellowhigh(class int,ranks int)
insert into sellowhigh values(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,4),(3,1),(3,2),(3,3),(3,7),(4,2),(4,3),(4,5),(4,8)
select * from sellowhigh

select class,min(ranks)as ranks from sellowhigh group by class union
select class,max(ranks)as ranks from sellowhigh group by class


class Ranks
1 1
1 4
2 1
2 4
3 1
3 7
4 2
4 8

I hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
Christian Graus 3-Jan-14 17:12pm    
select class, min(ranks) as minValue, max(ranks) as maxValue from sellowhigh group by class will work just fine.
♥…ЯҠ…♥ 3-Jan-14 23:02pm    
Look at the result, he dont want to display as min, max column, he wants it to be in next record for every class
Christian Graus 3-Jan-14 23:11pm    
You're right. So what you're actually missing, is an order by :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900