I have a data like below:
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
I need to pull a record that has the previous month statement end date if the past_due_date is a current month like:
Past_due_date. statement_begin_date statement_end_dt
2018-07-10 2018-05-11 2018-06-10
I am currently using this function but its matching the record only for the current month ie YYYYMM of statement_ed_date = YYYYMM of past_due date, but i want something like, YYYYMM of past_due_dt= Statment_end_dt-1 month
CAST(cast(A.STMT_END_DT as date format 'YYYYMM') as CHAR(6))=CAST(cast(b.Past_due_fee_dt as date format 'YYYYMM') as CHAR(6))
Ps it doesnt have to be in YYYYMM format
Instead of converting to a string you better compare to the 1st of month:
where trunc(add_months(Past_due_fee_dt,-1), 'm') = trunc(STMT_END_DT, 'm')
But to compare to the current & previous month you can simply calculate begin/end of the months:
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
I tried this and it worked for my case
CAST(cast(b.Past_due_fee_dt as date format 'YYYY') as VARCHAR(4))=CAST(cast(a.stmt_end_dt as date format 'YYYY') as VARCHAR(4)) and (CAST(cast(B.Past_due_fee_dt as date format 'MM') as VARCHAR(2))-CAST(cast(a.stmt_end_dt as date format 'MM') as VARCHAR(2)))=1
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.