是否可以在Hive中检索上一个星期六的日期。
需要此格式的输出,即“2014-11-01”。
每当运行查询时,它必须返回上一个星期六的日期。
答案 0 :(得分:0)
这样做
select CASE
WHEN from_unixtime(unix_timestamp(),'EEEE')="Friday"
THEN Date_add(CAST(from_unixtime(unix_timestamp()-1*60*60*24,'yyyy-MM-dd') as String),-5)
WHEN from_unixtime(unix_timestamp(),'EEEE')="Thursday"
THEN Date_add(CAST(from_unixtime(unix_timestamp()-1*60*60*24,'yyyy-MM-dd') as String),-4)
WHEN from_unixtime(unix_timestamp(),'EEEE')="Wednesday"
THEN Date_add(CAST(from_unixtime(unix_timestamp()-1*60*60*24,'yyyy-MM-dd') as String),-3)
WHEN from_unixtime(unix_timestamp(),'EEEE')="Tuesday"
THEN Date_add(CAST(from_unixtime(unix_timestamp()-1*60*60*24,'yyyy-MM-dd') as String),-2)
WHEN from_unixtime(unix_timestamp(),'EEEE')="Monday"
THEN Date_add(CAST(from_unixtime(unix_timestamp()-1*60*60*24,'yyyy-MM-dd') as String),-1)
WHEN from_unixtime(unix_timestamp(),'EEEE')="Sunday"
THEN Date_add(CAST(from_unixtime(unix_timestamp()-1*60*60*24,'yyyy-MM-dd') as String),0)
ELSE Date_add(CAST(from_unixtime(unix_timestamp()-1*60*60*24,'yyyy-MM-dd') as String),-6)
END
from fun;
输出
2014-11-01
答案 1 :(得分:0)
Danila Ganchar是对的。这是寻找上周六的最聪明,最快捷的方式。
SELECT date_sub(from_unixtime(unix_timestamp()),cast(from_unixtime(unix_timestamp(),' u')AS int)+ 1);
答案 2 :(得分:-1)
以下查询将解决您的问题:
SELECT date_sub(CURRENT_DATE, cast(from_unixtime(unix_timestamp(), 'u') AS int) + 1);