If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > To get a new column while selecting records from table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-22-06, 08:56
hvijayakumar hvijayakumar is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
To get a new column while selecting records from table

Hi,

I am a new member of this forum and also beginner to database. Please let me know ,how to write a query for the below question.

There is a table with following details.
Name Total marks
AAA 300
BBB 200
CCC 500
DDD 150
EEE 600

I want this detail to be arranged in descending order of total marks.And a new column should show the order of the result set, ranked from highest to lowest, in the order of Total marks.

i.e.,
Output should looklike as follows,
Name Total marks Rank
EEE 600 1
CCC 500 2
AAA 300 3
BBB 200 4
DDD 150 5

Query:
Select name,total marks from studenttable orderby 2.
I want to know ,what to include with this query to get the rank column.
Reply With Quote
  #2 (permalink)  
Old 08-22-06, 11:40
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Look in the SQL Reference manual for the OLAP function row_number.

Andy
Reply With Quote
  #3 (permalink)  
Old 08-23-06, 01:06
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
The following will also work:
Code:
SELECT a.name, a.total_marks, count(*)
FROM   student_table AS a INNER JOIN student_table AS b
       ON a.total_marks <= b.total_marks
GROUP BY a.name, a.total_marks
HAVING  count(*) < 6
ORDER BY 2 DESC
There is nothing specific to DB2 in this solution, though: this is standard SQL (which works with DB2 from at least version 7 on).
Starting version 8, though, there is indeed a more elegant (standard SQL) solution using OLAP functions.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 08-23-06 at 03:29.
Reply With Quote
  #4 (permalink)  
Old 08-23-06, 10:36
hvijayakumar hvijayakumar is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks

Thanks...Peter.Vanroose and Andy......
Really your answers worked well for me.
thanks a llot...
Reply With Quote
  #5 (permalink)  
Old 08-23-06, 14:28
msateeshkumar msateeshkumar is offline
Registered User
 
Join Date: May 2003
Location: Phoenix, US
Posts: 39
Below would be the query using OLAP function
db2 "Select Name,TotalMarks ,rank () over(order by totalmarks desc) from table"
__________________
OCP,IBM UDB,SYBASE DBA
TCS, BANGALORE, INDIA
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On