I had a scenario where I wanted to calculate week ending date (given a date), considering week ends at Saturday. I read some answers but could not find the solution, so I am posting my approach here. Hope it helps somebody like me.
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;
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.