2016-03-03 176 views
0

我有一個場景,我想計算周結束日期(給定一個日期),考慮週週末結束。我讀了一些答案,但找不到解決方案,所以我在這裏發佈我的方法。希望它能幫助像我這樣的人。如何使用星期六作爲星期結束日期來計算星期結束日期?

select 
TransactionDateTime, 
from_unixtime(unix_timestamp(TransactionTime),'u') as wkday, 
date_add (TransactionDateTime, 
case when from_unixtime(unix_timestamp(TransactionTime),'u')=7 then 6 
when from_unixtime(unix_timestamp(TransactionTime),'u')=1 then 5 
when from_unixtime(unix_timestamp(TransactionTime),'u')=2 then 4 
when from_unixtime(unix_timestamp(TransactionTime),'u')=3 then 3 
when from_unixtime(unix_timestamp(TransactionTime),'u')=4 then 2 
when from_unixtime(unix_timestamp(TransactionTime),'u')=5 then 1 
when from_unixtime(unix_timestamp(TransactionTime),'u')=6 then 0 
end) as UsageWkEndDt 
from TransTable; 
+0

請刪除你的答案 – Yossi

+0

你給實際的輸入格式沒有例子,和預期產出沒有例子,例如*「給出一個字符串'2016-03-03 21:05'指向一個星期四,我期待一個字符串'2016-03-05'即下一個或相等的星期六」* –

+0

而且你不給Hive版本 - 從V1.2.0開始,你有內置函數'next_day(string start_date,string day_of_week)'cf. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions –

回答

0
select 
TransactionDateTime, 
from_unixtime(unix_timestamp(TransactionTime),'u') as wkday, 
date_add (TransactionDateTime, 
case when from_unixtime(unix_timestamp(TransactionTime),'u')=7 then 6 
when from_unixtime(unix_timestamp(TransactionTime),'u')=1 then 5 
when from_unixtime(unix_timestamp(TransactionTime),'u')=2 then 4 
when from_unixtime(unix_timestamp(TransactionTime),'u')=3 then 3 
when from_unixtime(unix_timestamp(TransactionTime),'u')=4 then 2 
when from_unixtime(unix_timestamp(TransactionTime),'u')=5 then 1 
when from_unixtime(unix_timestamp(TransactionTime),'u')=6 then 0 
end) as UsageWkEndDt 
from TransTable; 
相關問題