'Finding Max date in hive

I have a column named "Date" which is in string datatype. 01-24-2018 04-30-2017

How to find the maximum of these dates which is in string?

I used this query which is not returning the expected max.

max(to_date(from_unixtime(unix_timestamp(b.Date,'MM-dd-yyyy')))


Solution 1:[1]

Try

MAX( cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)) 

EDIT: In your comments you mentioned you need to get the account details for the max date.You could use this.

SELECT a.id,
       b.contract,
       CAST (
          TO_DATE (
             from_unixtime (unix_timestam??p (b.date, 'MM-dd-yyyy'))) AS DATE)
          AS MAX_DATE
  FROM acct a JOIN customer b ON (b.partyid = a.offerid)
 WHERE CAST (
          TO_DATE (
             from_unixtime (unix_timestam??p (b.date, 'MM-dd-yyyy'))) AS DATE) IN 

  ( SELECT 
         MAX (
            CAST (
               TO_DATE (
                  from_unixtime (unix_timestam??p (b.date, 'MM-dd-yyyy'))) AS DATE))
    FROM acct a JOIN customer b ON (b.partyid = a.offerid)
   WHERE b.contract = 200427747 );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1