MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sql query for getting max value based on other column

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
maverick05
Beginner


Joined: 15 May 2005
Posts: 72
Topics: 24

PostPosted: Sun Apr 19, 2009 10:09 pm    Post subject: Sql query for getting max value based on other column Reply with quote

Hi,

I have the following 3 rows in db2 table.Table has 3 columns X,Y,Z

Code:

X Y Z
A 1 2001-01-01
A 2 2001-01-01
A 3 2002-02-02

Now my req is that i need to fetch the row with max Y column value(max(Y)) if and only if date 2001-01-01 is present in any of the 3 rows(Note that column1 value is same for all the 3 rows)

How can i achieve the result using a simple query.if not possible with a single query,what is the best query can be written to get the desired output.

so my output should be the last row with max column Y value .i.e 3
Thanks for your help
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Sun Apr 19, 2009 10:29 pm    Post subject: Reply with quote

maverick05,

Try this untested sql

Code:

SELECT X
      ,MAX(Y)
  FROM TABLE
 WHERE X IN (SELECT X FROM TABLE WHERE Z = '2001-01-01')
 GROUP BY X
;


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
maverick05
Beginner


Joined: 15 May 2005
Posts: 72
Topics: 24

PostPosted: Mon Apr 20, 2009 7:22 am    Post subject: Reply with quote

Hi Kolusu,i need to fetch the third row but not the second row.I think the above sql will fetch MAx(Y) as 2 which is 2nd row.Thanks
Back to top
View user's profile Send private message
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Mon Apr 20, 2009 9:41 am    Post subject: Reply with quote

Try:
SELECT X, MAX(Y) FROM TABLE
WHERE X IN(SELECT X FROM TABLE
WHERE Z = '2001-01-01')
GROUP BY X
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Mon Apr 20, 2009 10:19 am    Post subject: Reply with quote

maverick05,

What makes you think my sql will fetch the second row? Did you even try to run?

Danm ,

Isn't it the same sql I have shown?

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Mon Apr 20, 2009 11:17 am    Post subject: Reply with quote

Kolusu,

Opps! It is the same.
Back to top
View user's profile Send private message
maverick05
Beginner


Joined: 15 May 2005
Posts: 72
Topics: 24

PostPosted: Mon Apr 20, 2009 1:04 pm    Post subject: Reply with quote

Thanks kolusu.it worked
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group