我有如下数据:
tag = security_group.create_tags(Tags=[{'Key': 'Name','Value': SECURITY_GROUP_NAME},])
如果past_due_date是当前月份,则需要提取具有前一个月语句结束日期的记录,例如:
Past_due_date. statement_begin_date statement_end_dt
2018-07-10 2018-04-11 2018-05-10
2018-07-10 2018-05-11 2018-06-10
2018-07-10 2018-06-11 2018-07-10
2018-07-10 2018-07-11 2018-08-10
我当前正在使用此功能,但它仅与当月的记录匹配,即statement_ed_date的YYYYMM =过去日期的YYYYMM,但是我想要的过去Paste_due_dt的YYYYMM = Statment_end_dt-1个月
Past_due_date. statement_begin_date statement_end_dt
2018-07-10 2018-05-11 2018-06-10
P.s。不必为YYYYMM格式
答案 0 :(得分:2)
与转换为字符串相比,您最好将其与本月的第一天进行比较:
where trunc(add_months(Past_due_fee_dt,-1), 'm') = trunc(STMT_END_DT, 'm')
但是要与当前月份和上个月进行比较,您可以简单地计算月份的开始/结束时间:
where Past_due_fee_dt between trunc(current_date, 'm') -- 1st day of current month
and last_day(current_date) -- last day of current month
and STMT_END_DT between trunc(add_months(current_date, -1), 'm') -- 1st day of previous month
and last_day(add_months(current_date, -1)) -- last day of previous month
答案 1 :(得分:0)
我尝试过了,对我来说很有效
CAST(cast(b.Past_due_fee_dt为日期格式'YYYY')为VARCHAR(4))= CAST(cast(a.stmt_end_dt为日期格式'YYYY')为VARCHAR(4))和 (CAST(cast(cast.B.Past_due_fee_dt为日期格式'MM')为VARCHAR(2))-CAST(cast(cast(a.stmt_end_dt为日期格式'MM')为VARCHAR(2)))= 1