View previous topic :: View next topic |
Author |
Message |
Piscesian Beginner
Joined: 17 Dec 2004 Posts: 14 Topics: 8 Location: Monrovia,California
|
Posted: Sun Dec 02, 2007 12:51 am Post subject: Select row with Maximum sequence value |
|
|
Hi,
I have a DB2 table in which rows are stored in the following manner
Code: |
SDNum SeqNum Textdata
84510 4 dddd
84510 3 cccc
84510 2 bbbb
84510 1 aaaa
84511 3 cccc
84511 2 bbbb
84511 1 aaaa
84512 2 yyyy
84512 1 xxxx
|
I need all the SDNum rows that have maximum SeqNum value
Considering the above example, output of SQL should be
Code: |
84510 4 dddd
84511 3 cccc
84512 2 yyyy
|
Can anyone suggest how to achieve this?
Thanks, |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Sun Dec 02, 2007 3:11 am Post subject: |
|
|
Piscesian,
Try this untested sql
Code: |
SELECT *
FROM TABLE
WHERE (SDNUM,SEQNUM) IN (SELECT SDNUM
,MAX(SEQNUM)
FROM TABLE
GROUP BY SDNUM)
;
|
Hope this helps...
Cheers _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
vivek1983 Intermediate
Joined: 20 Apr 2006 Posts: 222 Topics: 24
|
Posted: Sun Dec 02, 2007 11:17 pm Post subject: |
|
|
Piscesian,
I guess the following will work also:
Code: |
SELECT SDNUM, MAX(SEQNUM) AS SEQNUM, TEXTDATA FROM TABLE GROUP BY SDNUM
|
_________________ Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay) |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Mon Dec 03, 2007 11:13 am Post subject: |
|
|
vivek1983 wrote: | Piscesian,
I guess the following will work also:
Code: |
SELECT SDNUM, MAX(SEQNUM) AS SEQNUM, TEXTDATA FROM TABLE GROUP BY SDNUM
|
|
Vivek,
Please TEST before you post. Your SQL will NOT work as the grouping is inconsistent. You should have a group by clause on both SDNUM and Textdata
Hope this helps...
Cheers _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
|
|