WITH qq1 AS (
SELECT
MAX(month_end_dt) AS max_month_end_dt
FROM
ods.bde_loan_mly
),
qq2 AS (
SELECT
UDF_ADD_MONTHS(current_date, -1) AS curr_date
FROM
DUAL
),
qq3 AS (
SELECT
TO_CHAR(qq2.curr_date, 'MM/yyyy') AS MONTH
FROM
DUAL
),
qq4 AS (
SELECT
MAX(history_dt) AS max_history_dt
FROM
BUWHSE_RAW.cpipassport
WHERE
TO_CHAR(HISTORY_DT, 'MM/yyyy') = (qq3.MONTH)
),
qq5 AS (
SELECT
MIN(HISTORY_DT) AS min_history_dt
FROM
BUWHSE_RAW.CPIPASSPORT
WHERE
TO_CHAR(HISTORY_DT, 'MMYYYY') = TO_CHAR(current_date, 'MMYYYY')
),
qq6 AS (
SELECT
MAX(MONTH_END_DT) AS max_month_end_dt_2
FROM
ODS.BDE_BORROWER_MLY
),
qq7 AS (
SELECT
MAX(MONTH_END_DT) AS max_month_end_dt_3
FROM
ods.bde_user_defined_mly
),
qq8 AS (
SELECT
TRUNC(LAST_DAY(add_months(current_date, -1))) AS last_day
FROM
sys.dual
)
SELECT
*
FROM
(
SELECT
ODS.BDE_LOAN_MLY.LN_NO,
ODS.BDE_LOAN_MLY.IV_CAT_CD,
ODS.BDE_LOAN_MLY.IV_ID,
ODS.BDE_LOAN_MLY.LN_NXT_PMT_DUE_DT,
ODS.BDE_LOAN_MLY.LN_SUSPENSE_BA,
ODS.BDE_LOAN_MLY.LN_1ST_PRIN_BA,
ODS.BDE_LOAN_MLY.LN_ANN_INT_RT,
ODS.BDE_LOAN_MLY.LN_HI_TY,
ODS.BDE_LOAN_MLY.LN_LO_TY,
ODS.BDE_LOAN_MLY.LN_MATURES_DT,
ODS.BDE_LOAN_MLY.LN_ARM_CD,
ODS.BDE_LOAN_MLY.LN_INT_DUE_BA,
ODS.BDE_LOAN_MLY.LN_CRE_QUL_ORIG_CD,
ODS.BDE_LOAN_MLY.LN_FC_WKST_STAT_CD,
ODS.BDE_LOAN_MLY.ELOC_CD,
ODS.BDE_LOAN_MLY.MONTH_END_DT,
ODS.BDE_LOAN_MLY.LN_BKR_STATUS_CD,
ODS.BDE_LOAN_MLY.LN_DIST_TYPE1_INT_ONLY_FLAG,
ODS.BDE_LOAN_MLY.LN_DIST_TYPE1_INT_ONLY_EXP_DT,
CALL_RPT_MAPPING.APP_ID,
CALL_RPT_MAPPING.SCHED_ID,
CALL_RPT_MAPPING.LINE,
CALL_RPT_MAPPING.COLUMN_VAL,
CALL_RPT_MAPPING.DESCR,
CALL_RPT_MAPPING.REM_TERM,
CALL_RPT_MAPPING.DELQ_DAYS,
CALL_RPT_MAPPING.FICO_SCORE_BUCKETS,
CALL_RPT_MAPPING.FORMULA_CD
FROM
ODS.BDE_LOAN_MLY,
SVC_FINANCE_ODS.CALL_RPT_MAPPING,
qq1
WHERE
MONTH_END_DT = qq1.max_month_end_dt
AND app_id = 'L'
AND INSTR(CODE_VALUES, IV_ID) <> 0
) a
LEFT JOIN (
SELECT
BUWHSE_RAW.CPIPASSPORT.history_dt,
BUWHSE_RAW.CPIPASSPORT.loan_number,
BUWHSE_RAW.CPIPASSPORT.BILLING_ADDRESS_LINE_1,
BUWHSE_RAW.CPIPASSPORT.BILLING_ADDRESS_LINE_2,
BUWHSE_RAW.CPIPASSPORT.BILLING_ADDRESS_LINE_3,
BUWHSE_RAW.CPIPASSPORT.BILLING_ADDRESS_LINE_4,
BUWHSE_RAW.CPIPASSPORT.BILLING_CITY_STATE,
BUWHSE_RAW.CPIPASSPORT.BILLING_ZIP_CODE,
BUWHSE_RAW.CPIPASSPORT.SEG_EL_CURR_AVAIL_LIMIT_AMOUNT,
BUWHSE_RAW.CPIPASSPORT.SEG_EL_LIEN_POSITION_NUMBER,
BUWHSE_RAW.CPIPASSPORT.LOAN_TERM,
BUWHSE_RAW.CPIPASSPORT.SEG_EL_CREDIT_LINE_AMOUNT
FROM
BUWHSE_RAW.CPIPASSPORT,
qq4
WHERE
HISTORY_DT = qq4.max_history_dt
) b ON a.LN_NO = b.loan_number
LEFT JOIN (
SELECT
BUWHSE_RAW.CPIPASSPORT.loan_number,
BUWHSE_RAW.CPIPASSPORT.FIRST_UNAMORTIZED_FEES_AMOUNT
FROM
BUWHSE_RAW.CPIPASSPORT,
qq5
WHERE
HISTORY_DT = qq5.min_history_dt
) c ON a.LN_NO = c.loan_number
LEFT JOIN (
SELECT
LN_NO,
DECODE (BO_RELATION_CD, 'X', 'X', null) AS MTGR_RELATIONSHIP_CODE,
BO_SHORT_NM AS CUST_SHORT_NM
FROM
ODS.BDE_BORROWER_MLY,
qq6
WHERE
MONTH_END_DT = qq6.max_month_end_dt_2
) d ON a.LN_NO = d.LN_NO
LEFT JOIN (
SELECT
LN_NO,
USR_01_POS4B_XX AS FRAUD_INDICATOR
FROM
ods.bde_user_defined_mly,
qq7
WHERE
USR_01_POS4B_XX = 'Y'
AND MONTH_END_DT = qq7.max_month_end_dt_3
) e ON a.LN_NO = e.LN_NO
LEFT JOIN (
SELECT
A.LN_NO,
B.OG_MTG_AM AS ORIGINAL_MORTGAGE_AMOUNT,
A.ORIG_LTV_RATIO_FC AS ORIGINAL_LOAN_TO_VALUE_RATIO,
CASE WHEN GREATEST(NVL(C.PR_VALUE_AM, 0), NVL(B.OG_PROP_VAL_AM, 0)) = 0 THEN D.APPRAISAL
ELSE GREATEST(NVL(C.PR_VALUE_AM, 0), NVL(B.OG_PROP_VAL_AM, 0))
END AS APPRAISAL_AMT,
B.OG_FHLBB_PUR_PR_AM AS FHLBB_PURCHASE_PRICE,
DECODE_UDF(
TRIM(A.LN_LO_TY),
1,
'FHA RESIDENTIAL',
2,
'VA RESIDENTIAL',
3,
'CONVENTIONAL RES WITHOUT PMI',
4,
'COMMERCIAL',
5,
'FHA PROJECT',
6,
'CONVENTIONAL RES WITH PMI',
7,
'HUD 235/265',
8,
'UNASSIGNED',
9,
'FARM LOAN'
) AS LO_TYPE_DESCRIPTION
FROM
ODS.BDE_LOAN_MLY A,
ODS.BDE_ORIGINAL_LOAN_MLY B,
ODS.BDE_PROPERTY_MLY C,
BUREPORTS.LQ_LOAN_TAPE_FIELDS D,
qq8
WHERE
A.MONTH_END_DT = B.MONTH_END_DT
AND A.LN_NO = B.LN_NO
AND A.MONTH_END_DT = C.MONTH_END_DT
AND A.LN_NO = C.LN_NO
AND TO_CHAR(A.LN_NO) = D.LOAN_NUMBER (+)
AND A.IV_ID between '100'
and '189'
and A.LN_1ST_PRIN_BA > 0
AND A.MONTH_END_DT = qq8.last_day
) f ON a.LN_NO = f.LN_NO
LEFT JOIN (
SELECT
A.LN_NO LN_NO,
A.AR_ALTNG_AM_CLC_FG ARM_NEG_AMORT_IND
FROM
ODS.BDE_NEW_ARM_MLY A,
qq8
WHERE
A.MONTH_END_DT = qq8.last_day
) g ON a.LN_NO = g.LN_NO