How to get previous day date in Hive

徘徊边缘 提交于 2019-12-12 10:25:00

问题


I am novice to Hive. Trying to get the previous day date using the below query:

SELECT MAX(id) FROM store_rcd_table
WHERE recon_dt = unix_timestamp(date_sub(from_unixtime(unix_timestamp(),
'yyyy-MM-dd'),1),'yyyy-MM-dd')

but getting the NULL as output. The output should have been date(2017-09-23) and MAX(id).

Also tried,

Select MAX(id) FROM store_rcd_table
WHERE recon_dt ='2017-09-24';

No output for this query also, just OK is coming as an output.

Not getting what the issue is? Any suggestion/ help is appreciated.

The structure of the table is:

id              string                                      
locationid      string                                      
mngrid          string                                      
empid           string                                                                          
deleted         boolean                                     
recon_dt        string                                      

回答1:


Try select date_sub(current_date, 1);

0: jdbc:hive2://hiveserver2:1> select date_sub(current_date, 1);
+-------------+--+
|     _c0     |
+-------------+--+
| 2017-09-24  |
+-------------+--+
1 row selected (0.182 seconds)

Original attempt was date_sub(CAST(unix_timestamp()*1000 AS TIMESTAMP), 1) but apparently that's wrong. See comments

Feel free to read up on Hive Date Functions


If you get no results, you should check the source data to see what recon_dt actually is. (Also worth pointing out that you used two different tables in the question)



来源:https://stackoverflow.com/questions/46415615/how-to-get-previous-day-date-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!