How to get previous day date in Hive

2020-08-09 05:19发布

问题:

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)