首页 > 解决方案 > 选择最新记录

问题描述

这是我的数据集。

我想运行一个 select 语句,该语句运行并仅通过 Recored_timestampe 字段为键 teacher_id 和 student_id 选择最新记录。所以任何时候,它运行时只需要提供一条记录。请问我该怎么做?输出可能没有字段 Recored_timestamp。谢谢

标签: sqlhiveql

解决方案


使用窗口函数,按teacher_id 和student_id 分区并按recorded_timestamp 对其进行排序将为您提供所需的结果。

select * from(select teacher_id,student_id,teacher_name,comment ,recorded_timestamp, row_number() over(partition by teacher_id,student_id order by recorded_timestamp desc)as rownum from temp0607)out1 where rownum=1

此外,您可能需要查看记录的时间戳的存储方式。如果它存储为字符串,您可以使用将其转换为时间戳from_unixtime(unix_timestamp(recorded_timestamp,'dd/MM/yyyy HH:mm'),'dd/MM/yyyy HH:mm')


推荐阅读