Max string value in DB2

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
jweir
Participant
Posts: 134
Joined: Wed Aug 04, 2010 12:31 pm

Max string value in DB2

Post by jweir »

Hello,

I need a query in DB2 where it has to select the maximum value in a string field. I have to select two other fields, in addition to the field that needs the max function performed on it. Here is a sample test that we have tried:

Code: Select all

Select MAX(EXPIRE_DATE), CUSTNUM, ZH4IN from RTL.STG_MEDICAL_TABLE
group by (CUSTNUM, ZH4IN, EXPIRE_DATE)
This returns records with the same CUSTNUM with different EXPIRE_DATE fields. I only need one record with the max EXPIRE_DATE.

Any suggestions?

Thanks in advance
Jweir

--- If strength were all, tiger would not fear scorpion.
vinothkumar
Participant
Posts: 342
Joined: Tue Nov 04, 2008 10:38 am
Location: Chennai, India

Post by vinothkumar »

Remove EXPIRE_DT from GROUP BY.
Mike
Premium Member
Premium Member
Posts: 1021
Joined: Sun Mar 03, 2002 6:01 pm
Location: Tampa, FL

Post by Mike »

Code: Select all

SELECT T1.EXPIRE_DATE,
       T1.CUSTNUM,
       T1.ZH4IN
FROM   RTL.STG_MEDICAL_TABLE T1
WHERE  T1.EXPIRE_DATE = SELECT MAX(T2.EXPIRE_DT)
                        FROM   RTL.STG_MEDICAL_TABLE T2
Mike
jweir
Participant
Posts: 134
Joined: Wed Aug 04, 2010 12:31 pm

Post by jweir »

Code: Select all

SELECT T1.EXPIRE_DATE, 
       T1.CUSTNUM, 
       T1.ZH4IN 
FROM   RTL.STG_MEDICAL_TABLE T1 
WHERE  T1.EXPIRE_DATE = SELECT MAX(T2.EXPIRE_DT) 
                        FROM   RTL.STG_MEDICAL_TABLE T2
This worked Mike! Thanks a mil!
Jweir

--- If strength were all, tiger would not fear scorpion.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Don't you need the maximum expiry date for each CUSTNUM, ZH41N combination? This would need more conditions in the where clause, turning the query into a correlated subquery.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
jweir
Participant
Posts: 134
Joined: Wed Aug 04, 2010 12:31 pm

Post by jweir »

Just for CUSTNUM.
Jweir

--- If strength were all, tiger would not fear scorpion.
Post Reply