Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old January 28th, 2004, 10:17 PM
vaninagam vaninagam is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 vaninagam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
finding rank of the student in student table

Hi,

I have a table for student information with the following fields.

student id
student name
subject 1
subject 2
subject 3

I need to write a query so that the student rank should display.

for example :
let this is the data I have :

select * from student

student id studentname subject 1 subject 2 subject 3
1 xyz 68 78 88
2 abc 60 80 90
3 test 90 90 95


Now my query should give me the rank of the students:


student id student name Rank
3 test 1
1 xyz 2
2 abc 3

Thanks in advance.

Reply With Quote
  #2  
Old January 29th, 2004, 04:30 PM
corillio corillio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 corillio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
do you mean you need the average of the "subjects" and then ordered by highest to lowest ?

Reply With Quote
  #3  
Old January 29th, 2004, 04:34 PM
corillio corillio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 corillio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
if your "subject 1, 2, 3" are scores and you want to order them highest to lowest. then you will need to get the AVG of the scores then do an ORDERBY that AVG.

Reply With Quote
  #4  
Old January 29th, 2004, 09:48 PM
vaninagam vaninagam is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 vaninagam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I would like to have the total (Sub1+ sub2+sub3), order by total, then assign rank
eg :
If this is the out put that would be great.

student id student name Rank
3 test 1
1 xyz 2
2 abc 3

Thanks for your time.

Reply With Quote
  #5  
Old January 30th, 2004, 07:53 AM
corillio corillio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 corillio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
okay so then have a field....
total = Sub1 + Sub2 + Sub3
then have your select be...

select * from tablename orderby total desc;

Reply With Quote
  #6  
Old January 31st, 2004, 06:22 PM
Dan Drillich Dan Drillich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 Dan Drillich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I'm not sure how you would like to take care of the case in which two students have the same total.
I added student #4 and she has the same count as student #3.


Please try (I use Oracle 9.2.0.4) :

create table students (

student_id NUMBER,
studentname VARCHAR(20),
subject1 NUMBER,
subject2 NUMBER,
subject3 NUMBER

)



insert into students values (1, 'xyz', 68, 78, 88 )
insert into students values (2, 'abc', 60, 80, 90)
insert into students values (3, 'test1', 90, 90, 95)
insert into students values (4, 'test2', 89, 91, 95)


select student_id, s,d, rownum
from
(
select student_id, s,
rank() over (partition by s order by student_id) d
from (
select student_id, (subject1 + subject2 + subject3) s
from students
)
order by s
)


which returns:

STUDENT_ID S D ROWNUM

2 ------- 230 ------- 1 ------- 1
1 ------- 234 ------- 1 ------- 2
3 ------- 275 ------- 1 ------- 3
4 ------- 275 ------- 2 ------- 4


Hope it helps,
Dan

Reply With Quote
  #7  
Old January 31st, 2004, 06:57 PM
Dan Drillich Dan Drillich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 Dan Drillich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Please try also:

select student_id, s, rank() over (partition by c order by s) d, rownum
from
(
select student_id, s, 7 c
from (
select student_id, (subject1 + subject2 + subject3) s
from students
)
order by s
)

which returns:

STUDENT_ID S D ROWNUM

2 ------- 230 ------- 1------- 1
1 ------- 234 ------- 2------- 2
3 ------- 275 ------- 3 ------- 3
4 ------- 275 ------- 3------- 4

Regards,
Dan

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > finding rank of the student in student table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway