有没有办法在 impala 中添加和减去日期中的天数

Is there way to add and subtract the number of days in dates in impala

我有要求 table A 有 cont_number、start_dates 和 end_dates 和 table B 有 cont_number,numberofdays(具有 + 和 - 值的天数 eg:30,-20) Table答:

cont_number start_date end_date
276820 01-Jul-2021 31-Jul-2021
817689 01-Jun-2021 30-Jun-2021
827628 01-Sep-2021 30-sep-2021

Table-B

cont_number Numberofdays
276820 -30
817689 40
827628 20

如果 Table B Numberofdays 具有 +ve 值,它应该将这些天数添加到 end_date of Table-A 并且如果它是 -ve 值那么它应该添加到 start_date 请帮助我满足以下要求

预期输出:

cont_number start_date end_date new_start_date new_end_date
276820 01-Jul-2021 31-Jul-2021 01-Jun-2021 31-Jul-2021
817689 01-Jun-2021 30-Jun-2021 01-Jun-2021 09-Aug-2021
827628 01-Sep-2021 30-sep-2021 01-Sep-2021 20-Oct-2021

您可以在 impala 中使用 ifcase-when

select 
a.cont_number cont_number,
a.start_date,
a.end_date,
if (b.Numberofdays>= 0 then a.end_date+ interval b.Numberofdays days,a.start_date) new_start_date,
if (b.Numberofdays< 0 then a.start_date+ interval b.Numberofdays days,a.end_date) new_end_date
from tablea A, tableb B
where a.cont_number=b.cont_number

new_start_date 的计算方式为 Numberofdays >=0 然后 end_date + Numberofdays 否则使用原始开始日期。
new_end_date 的计算方式为 Numberofdays <0 then start_date + Numberofdays else use original end date.