将时间戳转换为配置单元中的日期,并获取该日期的数据

myzjeezk  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(289)

我有一个这样的问题

select distinct emp,phno,addrs,email from cdv.emp;

现在我只想获取在最新生成日期创建的数据,而不是旧数据。我有一个审计专栏 created_on -这是唯一的密钥和时间戳
选择不同的emp,phno,addrs,email from cdv.emp;
我希望最新的数据基于 created_on(timestamp) 在24小时内生成的列或最大日期

kkbh8khc

kkbh8khc1#

使用 rank 分析函数。它的工作速度比子查询快得多:

select distinct emp,phno,addrs,email
from
(
select emp,phno,addrs,email,
       rank() over(order by to_date(c.created_on) desc) rn 
  from cdv.emp c
)s
where rn=1;

如果你想要最新的记录 emp,phno,addrs,email ,则可以使用不带distinct的行\u number()。如果此方法适用,则速度会更快:

select emp,phno,addrs,email
from
(
select emp,phno,addrs,email,
       row_number() over(partition by emp,phno,addrs,email order by to_date(c.created_on) desc) rn 
  from cdv.emp c
)s
where rn=1;
mzaanser

mzaanser2#

我尝试了这个选择不同的emp,phno,addrs,email from cdv.emp where to_date(created_on)in(select max(to_date(c.created_on))from cdv.emp c);
有没有其他更有效更快的方法?

相关问题