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

发布于 2025-01-11 04:39:42 字数 1387 浏览 1 评论 0原文

我有要求,其中表 A 具有 cont_number、start_dates 和 end_dates 并且 表 B 具有 cont_number,numberofdays(具有 + 和 - 值的天数,例如:30、-20) 表 A:

cont_numberstart_dateend_date
2768202021年7月1日 2021年7月31日
8176892021年6月1日 2021年6月30日
8276282021年9月01日 2021年9月30日

表-B

cont_numberNumberofdays
276820-30
81768940
82762820

如果表 B Numberofdays 具有 +ve 值,则应将这些天添加到表 A 的 end_date 中,如果是 -ve 值,则应添加到 start_date 请帮助我满足以下要求

期望输出:

cont_numberstart_dateend_datenew_start_datenew_end_date
27682001-Jul-202131-Jul-202101-Jun-202131-Jul-2021
81768901-Jun-202130-Jun- 2021年2021年6月1日 2021年8月9日
8276282021年9月1日 2021年9月30日 2021年9月1日 2021年10月20日

I have requirement where table A is having the cont_number,start_dates and end_dates and
table B is having cont_number,numberofdays(numbers of days having + and - values eg:30,-20)
Table A:

cont_numberstart_dateend_date
27682001-Jul-202131-Jul-2021
81768901-Jun-202130-Jun-2021
82762801-Sep-202130-sep-2021

Table-B

cont_numberNumberofdays
276820-30
81768940
82762820

If Table B Numberofdays having +ve value it should add those days to end_date of Table-A and if it is -ve value then it should add to start_date
Please help me with this below requirement

Expecting output:

cont_numberstart_dateend_datenew_start_datenew_end_date
27682001-Jul-202131-Jul-202101-Jun-202131-Jul-2021
81768901-Jun-202130-Jun-202101-Jun-202109-Aug-2021
82762801-Sep-202130-sep-202101-Sep-202120-Oct-2021

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2025-01-18 04:39:42

你可以在 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,则 start_date + Numberofdays,否则使用原始结束日期。

you can use if or case-when in impala.

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 is calculated as if Numberofdays >=0 then end_date + Numberofdays else use original start date.
new_end_date is calculated as if Numberofdays <0 then start_date + Numberofdays else use original end date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文