hive sql find the latest record

后端 未结 8 1992
别那么骄傲
别那么骄傲 2021-01-30 17:28

the table is:

create table test (
id string,
name string,
age string,
modified string)

data like this:

id    name   age  modife         


        
8条回答
  •  一生所求
    2021-01-30 18:07

    Just slightly different approach than what has been answered in previous answer.

    Below example uses hive windowing function to find out the latest record, read more here

    SELECT t.id
        ,t.name
        ,t.age
        ,t.modified
    FROM (
        SELECT id
            ,name
            ,age
            ,modified
            ,ROW_NUMBER() OVER (
                PARTITION BY id ORDER BY unix_timestamp(modified,'yyyy-MM-dd hh:mm:ss') DESC
                ) AS ROW_NUMBER   
        FROM test
        ) t
    WHERE t.ROW_NUMBER <= 1;
    

    The modified is string so converting it to timestamp using unix_timestamp(modified,'yyyy-MM-dd hh:mm:ss') then applying order by on timestamp.

提交回复
热议问题