hive查询

64jmpszr  于 2021-05-27  发布在  Hadoop
关注(0)|答案(3)|浏览(425)

我有一张表,上面有下列数据。

我希望需要返回的行是exp\u dt“2020-09-22”。但当在下面运行查询时,它将返回两行。我不明白为什么它还返回第一排时,它已经生效dt“2020-09-19”。

select id,cd,eff_dt,exp_dt,post_dt from table 
where from_unixtime(unix_timestamp(eff_dt,"yyyy-MM-dd")) <= from_unixtime(unix_timestamp("2020-09-21","yyyy-MM-dd"))
and from_unixtime(unix_timestamp(exp_dt,"yyyy-MM-dd")) >= from_unixtime(unix_timestamp("2020-09-21","yyyy-MM-dd"));

我的问题有什么问题吗?我期待第二排的人回来。

von4xj4u

von4xj4u1#

这是否抓住了边缘的情况下,同一天到期,并解决您的问题,在同一时间?

select id,cd,eff_dt,exp_dt,post_dt from table 
where 
    (from_unixtime(unix_timestamp(eff_dt,"yyyy-MM-dd")) <= from_unixtime(unix_timestamp("2020-09-21","yyyy-MM-dd"))
     and
     from_unixtime(unix_timestamp(exp_dt,"yyyy-MM-dd")) > from_unixtime(unix_timestamp("2020-09-21","yyyy-MM-dd"))
    )
    or
    (from_unixtime(unix_timestamp(eff_dt,"yyyy-MM-dd")) = from_unixtime(unix_timestamp("2020-09-21","yyyy-MM-dd"))
     and
     from_unixtime(unix_timestamp(exp_dt,"yyyy-MM-dd")) = from_unixtime(unix_timestamp("2020-09-21","yyyy-MM-dd"))
    )
;

事实上,我怀疑exp总是>=eff,所以可能只有一个条件

from_unixtime(unix_timestamp(eff_dt,"yyyy-MM-dd")) <= from_unixtime(unix_timestamp("2020-09-21","yyyy-MM-dd"))

够了吗。。。?

pn9klfpd

pn9klfpd2#

使用 < 作为比较 exp_date :

select id,cd,eff_dt,exp_dt,post_dt
from table 
where from_unixtime(unix_timestamp('2020-09-21', 'yyyy-MM-dd')) >= from_unixtime(unix_timestamp(eff_dt, 'yyyy-MM-dd')) and
      from_unixtime(unix_timestamp('2020-09-22', 'yyyy-MM-dd')) < from_unixtime(unix_timestamp(exp_dt, 'yyyy-MM-dd'))

我颠倒了对比顺序。我发现先用常量来遵循逻辑比较容易。

kr98yfug

kr98yfug3#

您不需要from\u unixtime(unix\u timestamp()),因为日期的格式已经正确,并且参数的格式与yyyy-mm-dd的格式相同。
查询中的问题是,您正在使用equal for eff和exp dates来查找日期上的最新记录使用此查询:

select id,cd,eff_dt,exp_dt,post_dt from table 
where eff_dt <= "2020-09-21"
  and exp_dt >  "2020-09-21";

如果只有日期(没有时间成分),则在scd2中当eff\u dt=exp\u dt时应该没有记录。只有在使用时间戳且时间不同时,日期才能相等,在本例中,请在检查之前将参数日期转换为时间戳。
scd2的设计应确保事实记录可以Map到scd2的一个记录。

相关问题