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

Need to retrieve the row with max SEQ_NR


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

New User


Joined: 01 Feb 2007
Posts: 30
Location: Mumbai

PostPosted: Thu Jan 24, 2008 1:02 pm
Reply with quote

i have the following table structure

ACCT_ID SEQ_NR
81256382 9999
81256382 1
81256382 3
79350011 9999
79350011 1
79350011 2

for each of the account id's I need to retrieve the row with max SEQ_NR
but row with SEQ_NR 9999 should not be retrieved.


PLease help me with the SQL query
Back to top
View user's profile Send private message
murmohk1

Senior Member


Joined: 29 Jun 2006
Posts: 1436
Location: Bangalore,India

PostPosted: Thu Jan 24, 2008 1:07 pm
Reply with quote

Tushar,

Customize to your need.

www.ibmmainframes.com/viewtopic.php?t=1472&highlight=maximum
Back to top
View user's profile Send private message
tushar_study

New User


Joined: 01 Feb 2007
Posts: 30
Location: Mumbai

PostPosted: Thu Jan 24, 2008 1:18 pm
Reply with quote

Hi MMohan,

thanks for the reply ...actually i m looking for sql using JOINS...can u give me an idea that if i dont wanna include 9999 then can i give SEQ_NR <> 9999 in the where clause of the join


Thanks and Regards,

Tushar
Back to top
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Thu Jan 24, 2008 3:19 pm
Reply with quote

Hi tushar,
i ve not executed this query however, you may check it out :

SELECT ACCT_ID, SEQ_NR FROM MYTABLE A WHERE SEQ_NR = (SELECT MAX(SEQ_NR) FROM MYTABLE B WHERE SEQ_NR <> 9999 AND A.SEQ_NR=B.SEQ_NR);
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Thu Jan 24, 2008 6:41 pm
Reply with quote

May need some tweeking.


Code:

--This grabs the max regardless of 9999 or not
SELECT acct_id, max(seq_nr) as max_nr
 FROM table
 GROUP BY acct_id

--Try this from a performance standpoint, I don't know if it will work
SELECT acct_id, max(seq_nr) as max_nr
 FROM table
 WHERE seq_nr <> 9999
 GROUP BY acct_id

--This should work but may be bad for performance
SELECT A.acct_id, max(A.seq_nr)
FROM
 (SELECT *
  FROM table
  WHERE seq_nr <> 9999) A
GROUP BY A.acct_id

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 Using Java/C/C++ to retrieve dataset ... Java & MQSeries 6
No new posts Retrieve IMS SubSystem Name IMS DB/DC 2
No new posts retrieve volume records from decollec... DFSORT/ICETOOL 4
No new posts Unable to retrieve Datasets Names usi... CLIST & REXX 20
No new posts Retrieve multiple records with metaco... CA Products 0
Search our Forums:

Back to Top