簡體   English   中英

如果 sysdate 大於 10 日,則 Hive-Query 以獲取從當月 10 日到當前日期的數據,否則從上月 10 日到 sysdate 查詢

[英]Hive-Query to get data from 10th of current month to current date if sysdate is greater than 10th else query from 10th of previous month to sysdate

我有一個要求,我需要查詢從當前日期到本月前 10 日的數據集。 示例 - 如果日期是今天的 1 月 27 日,那么查詢將考慮從 1 月 10 日到 1 月 27 日的數據。

如果當前日期小於本月 10 日,則查詢應考慮從上個月 10 日到當前日期的數據。 示例 - 如果日期是今天的 1 月 3 日,那么查詢應該考慮從 12 月 10 日到 1 月 3 日的數據。

我嘗試了以下查詢來獲取第一種情況的結果。

select * from my_table
where partition_col >= date_add(current_date,1 - day(current_date)+9)
and partition_col <= current_date;

任何人都可以幫助在單個查詢中管理第一個場景。

像這樣使用 CASE 表達式:

where partition_col >= case when day(current_date)>=10 
                            then date(concat(substr(current_date,1,7), '-10'))
                            else date(concat(substr(add_months(current_date,-1),1,7),'-10'))
                       end
     and partition_col <= current_date;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM