Getting Most Recent Date

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
kris007
Charter Member
Charter Member
Posts: 1102
Joined: Tue Jan 24, 2006 5:38 pm
Location: Riverside, RI

Getting Most Recent Date

Post by kris007 »

I have a requirement where I need to get the most recent date among all three different dates, the three different dates correspond to the 3 different date column in a DB2 Table.
Is there any function which gets the most recent date given three columns as input?
Thanks in Advance.
Kris

Where's the "Any" key?-Homer Simpson
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Kris, do it in the sql itself

Code: Select all

select max(maxdate) as maxdate 
from 
(select date1 as maxdate
  from table 
union 
select date2 as maxdate
  from table 
union 
select date3 as maxdate
  from table 
) as Temp
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Actually Kris, i just looked at my code again.
If you want it at a per table basis then my code will work. But if you want the max date from three columns at the row level then you need a case statement. Use this

Code: Select all

select 
(case 
  when date1 >= date2 and date1 >= date3 then date1 
  when date2 >= date3 then date2 
  else date3 
end ) As MaxDate
from table

THe above code will give you max date at the row level, and the code in my earlier post will give you max date at the table level.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
kris007
Charter Member
Charter Member
Posts: 1102
Joined: Tue Jan 24, 2006 5:38 pm
Location: Riverside, RI

Post by kris007 »

Thanks Dsguru. I was thinking if there is any builtin function to do.
Kris

Where's the "Any" key?-Homer Simpson
kris007
Charter Member
Charter Member
Posts: 1102
Joined: Tue Jan 24, 2006 5:38 pm
Location: Riverside, RI

Post by kris007 »

I actually have 10 different date columns and also I need the column name which has the maximum date.The following technique gets too labourious for 10 date columns even doing it in transformer gets tedious.

Code: Select all

select 
(case 
  when date1 >= date2 and date1 >= date3 then date1 
  when date2 >= date3 then date2 
  else date3 
end ) As MaxDate 
from table 
.
Is there any other way to do that, I am thinking of pivot stage but not really sure if it serves the purpose. Need suggestions from dsgurus here.
Thanks
Kris

Where's the "Any" key?-Homer Simpson
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

You can acheive the same using sime If-Then-Else in Transformer.
Or Pivot and a aggregator/sorter.
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Well, i dont think a function exists. In server there is Compare(), but that wont even work over here because the number of columns is more than 2.
As Kumar said, you can do this via if-then-else statements (lots of them for 10 columns).
I would go for the case statement in sql. I know its labourious, but i think its the cleanest.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
rwierdsm
Premium Member
Premium Member
Posts: 209
Joined: Fri Jan 09, 2004 1:14 pm
Location: Toronto, Canada
Contact:

Post by rwierdsm »

kris007 wrote:... and also I need the column name which has the maximum date...
Need to think about this one some more, the case statement will not provide the column name.

Kris, what are you going to do with the column name after you determine the max date?

Rob
Rob Wierdsma
Toronto, Canada
bartonbishop.com
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Ouch, totally bypassed that. Need the column name huh?
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
kris007
Charter Member
Charter Member
Posts: 1102
Joined: Tue Jan 24, 2006 5:38 pm
Location: Riverside, RI

Post by kris007 »

I didnot want to use if then else in the transformer as it is also as tedious as comparing in sql select stmt.
I was able to acheive this using

Code: Select all

XFM--->PIVOT--->SORT
in the transformer stage along with the date columns I am also outputting datecolumnnames as different columns.
Thanks guys for all your suggestions.You have been really helpful.
Kris

Where's the "Any" key?-Homer Simpson
Post Reply