我有一个要求,其中表A具有cont_number、start_dates和end_dates以及表B具有cont_number,numberofdays(具有+和-值的天数,例如:30,-20(表A:
cont_number | 开始日期 | 结束日期
---|---|
276820 | 2021年7月1日 | 2021月31日
817689 | 2021年6月1日 | 2021年6月30日
827628 | 2021年9月1日 | 2021年9月30日
您可以在黑斑羚中使用if
或case-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,则结束日期+天数,否则使用原始开始日期
new_end_date被计算为好像Numberofdays<0,然后开始日期+天数,否则使用原始结束日期。