IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Get MAXIMUM value in WHERE


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
harijax

New User


Joined: 16 Jul 2008
Posts: 22
Location: Bangalore

PostPosted: Thu Nov 20, 2008 10:22 am
Reply with quote

Hi,

I got a requirement as below.

I have a table like
Student Marks
AAAA 90
AAAA 86
AAAA 82
BBBB 92
BBBB 40
CCCC 80
DDDD 47
DDDD 89

My actual table is slightly more complex. I want to create a report of maximum marks of every student.

I wanted to fetch through a single SELECT.
AAAA 90
BBBB 92
CCCC 80
DDDD 89

Regards
harijax
Back to top
View user's profile Send private message
Prasanthhere

Active User


Joined: 03 Aug 2005
Posts: 306

PostPosted: Thu Nov 20, 2008 10:46 am
Reply with quote

How about

Code:

Select Name,Max(Marks) from tablename
group by Name
Back to top
View user's profile Send private message
darkstar13

New User


Joined: 06 Nov 2008
Posts: 46
Location: Manila, Philippines

PostPosted: Thu Nov 20, 2008 10:54 am
Reply with quote

Hi Harijax,

Using your table entries,
Code:
 SELECT STUDENT     
     , MAX ( MARKS )
  FROM TABLE_NAME   
 GROUP BY STUDENT ;


Should give the results you gave.
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Thu Nov 20, 2008 10:55 am
Reply with quote

Hi Hari,
You can use the following query

Select student,marks from std_table A
where marks = (select max(marks) from std_table B where B.Student=A.student);
Back to top
View user's profile Send private message
darkstar13

New User


Joined: 06 Nov 2008
Posts: 46
Location: Manila, Philippines

PostPosted: Thu Nov 20, 2008 10:58 am
Reply with quote

Hi again Hari,
You should give more specific details as to which tables are the columns STUDENT & MARKS from. I assumed it was from the same table.
Back to top
View user's profile Send private message
harijax

New User


Joined: 16 Jul 2008
Posts: 22
Location: Bangalore

PostPosted: Thu Nov 20, 2008 11:12 am
Reply with quote

Hi All, Slightly modified the requirement.
AAAA 90 Bangalore
AAAA 86 Chennai
AAAA 82 Bombay
BBBB 92 Kochi
BBBB 40 Delhi
CCCC 80 US
DDDD 47 UK
DDDD 89 Australia


I want to create a report as below again max(marks) is the main criteria.

AAAA Bangalore
BBBB Kochi
CCCC US
DDDD Australia

Srihari, can I have some other way to do it. using simple select.
Back to top
View user's profile Send private message
mallik4u

New User


Joined: 17 Sep 2008
Posts: 75
Location: bangalore

PostPosted: Thu Nov 20, 2008 12:23 pm
Reply with quote

Hi Hari,

I don't think you can get your results with out using sub query or joins. If you are looking for a join query then please find the below one. It worked for me.

SELECT A.NAME, A.CITY
FROM STUDENT A,
(SELECT NAME,MAX(MARKS) AS MARKS
FROM STUDENT GROUP BY NAME) B
WHERE A.NAME = B.NAME AND
A.MARKS = B.MARKS
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts SORT ERROR PARAMETER VALUE EXCEEDS M... DFSORT/ICETOOL 12
No new posts Increase the Maximum Length of LRECL ... JCL & VSAM 5
No new posts Logic to skip Maximum time-stamp and ... DB2 2
No new posts What is the maximum number of sort wo... SYNCSORT 2
No new posts How to find the maximum value of a pa... IMS DB/DC 8
Search our Forums:

Back to Top