0% found this document useful (0 votes)
15 views4 pages

LVS - Exist - Code 1

The document outlines a SQL script named 'cpa_bq_migration' that is designed to load final features required for model input into a target table. It includes a series of SQL commands to insert data from various sources based on a specified run date, and it handles errors by raising messages if no records are inserted. The script was created on October 18, 2024, and is located in the specified directory.

Uploaded by

pllkjx10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

LVS - Exist - Code 1

The document outlines a SQL script named 'cpa_bq_migration' that is designed to load final features required for model input into a target table. It includes a series of SQL commands to insert data from various sources based on a specified run date, and it handles errors by raising messages if no records are inserted. The script was created on October 18, 2024, and is located in the specified directory.

Uploaded by

pllkjx10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

-- -------------------------

+----------------------------------------------------------------------------------
--------------------------
-- Name | cpa_bq_migration
-- Description | Loads the final features required for model input
--
-- BSA |
-- Developer |
-- Created on | 2024-10-18
-- Location | pypl-edw/cpa/sql/cpa_bq_migration.sql
--
-- Input |
-- Parameters | run_date
-- Prev. set variables |
-- Tables, Views |
--
-- Output/Return code | Returns sql error code in case of a failure.
--
-- Updates History:
-- Date(yyyy-mm-dd) Modified by Comments
--
-----------------------------------------------------------------------------------
---------------------------------------------------

-- Beginning of procedural language


BEGIN

-- Insert the features into the target table


INSERT INTO pypl-edw.pp_scratch.cpa_bq_migration_tgt
WITH v6 AS (
SELECT
cc_number_hash,
v6_overall_model_approval_score AS scorelabel0,
v6_overall_model_nsf_decline_score AS scorelabel1,
v6_overall_model_other_decline_score AS scorelabel2,
run_date
FROM
pypl-edw.pp_engineering_views.issuer_decline_score_v6_1_refresh
WHERE
run_date = '${paypal_batch_date}'
AND cc_number_hash IS NOT NULL
),
stmpy_binhmac AS (
SELECT DISTINCT
bin_hmac,
txn_band_categ,
regulated_bank_y_n
FROM
pypl-edw.pp_cmt_access_views.issuer_decline_bin_hmac_lookup_vars
WHERE
run_date = '${paypal_batch_date}'
AND txn_band_categ = 'Total'
)
SELECT
mdlscore.cc_number_hash AS card_hmac,
facd.last_txn_dt,
facd.credit_card_created_date,
facd.bin_hmac,
facd.lifetime_success_count,
facd.last_success_txn_dt,
facd.max_success_txn_amt,
facd.lifetime_decline_count,
facd.last_decline_txn_dt,
facd.max_decline_txn_amt,
facd.cbt_lifetime_success_count,
facd.cbt_last_success_txn_dt,
facd.cbt_max_success_txn_amt,
facd.cbt_lifetime_decline_count,
facd.cbt_last_decline_txn_dt,
facd.cbt_max_decline_txn_amt,
facd.most_success_rcvr,
facd.most_success_rcvr_total_amt,
facd.most_success_rcvr_txn_count,
facd.total_success_rcvr_count,
facd.last_success_txn_id,
facd.last_success_txn_amt,
facd.last_success_txn_ts,
facd.last_success_txn_sndr_cntry_code,
facd.last_success_rcvr_id,
facd.last_success_rcvr_cntry_code,
facd.last_decline_txn_id,
facd.last_decline_txn_amt,
facd.last_decline_txn_ts,
facd.last_decline_txn_sndr_cntry_code,
facd.last_decline_rcvr_id,
facd.last_decline_rcvr_cntry_code,
facd.last_decline_cc_trans_status,
facd.last_decline_normalized_resp_code,
facd.cbt_last_success_txn_id,
facd.cbt_last_success_txn_amt,
facd.cbt_last_success_txn_ts,
facd.cbt_last_success_txn_sndr_cntry_code,
facd.cbt_last_success_rcvr_id,
facd.cbt_last_success_rcvr_cntry_code,
facd.cbt_last_decline_txn_id,
facd.cbt_last_decline_txn_amt,
facd.cbt_last_decline_txn_ts,
facd.cbt_last_decline_txn_sndr_cntry_code,
facd.cbt_last_decline_rcvr_id,
facd.cbt_last_decline_rcvr_cntry_code,
facd.cbt_last_decline_cc_trans_status,
facd.cbt_last_decline_normalized_resp_code,
facd.decline_cntr,
facd.stale_dcln_resp_rcvd_y_n,
bin.regulated_bank_y_n AS acqrr_resp_code,
CAST(`pypl-edw`.cw_udf.cw_round_half_even_bignumeric(CAST(mdlscore.scorelabel0
AS numeric), 4) AS numeric) AS model_score,
facd.model_variable,
mdlscore.run_date AS model_run_date,
'-99' AS v2_overall_model_approval_score,
'-99' AS v2_overall_model_nsf_decline_score,
'-99' AS v2_overall_model_other_decline_score,
'-99' AS v2_gt_500_model_approval_score,
'-99' AS v2_gt_500_model_nsf_decline_score,
'-99' AS v2_gt_500_model_other_decline_score,
'-99' AS v2_model_segment,
'-99' AS v2_1_overall_model_approval_score,
'-99' AS v2_1_gt_500_model_approval_score,
'NA' AS consu_engagmnt_seg_key,
'NA' AS txn_date_1,
'NA' AS retry_indicator_1,
'NA' AS txn_date_2,
'NA' AS retry_indicator_2,
'NA' AS txn_date_3,
'NA' AS retry_indicator_3,
'NA' AS txn_date_4,
'NA' AS retry_indicator_4,
'NA' AS txn_date_5,
'NA' AS retry_indicator_5,
'NA' AS txn_date_6,
'NA' AS retry_indicator_6,
'NA' AS txn_date_7,
'NA' AS retry_indicator_7,
'NA' AS txn_date_8,
'NA' AS retry_indicator_8,
'NA' AS txn_date_9,
'NA' AS retry_indicator_9,
'NA' AS txn_date_10,
'NA' AS retry_indicator_10,
facd.cre_ts,
facd.upd_ts,
facd.card_last_upd_dt,
facd.last_success_auth_txn_date,
CAST(0 AS numeric) AS strict_issuer_3ds_approval_score,
'-99' AS strict_issuer_non3ds_approval_score,
CAST(0 AS numeric) AS strict_issuer_3ds_decline_score,
CAST(0 AS numeric) AS strict_issuer_non3ds_decline_score,
'-99' AS v4_model_segment,
'-99' AS v4_overall_model_approval_score,
'-99' AS v4_overall_model_nsf_decline_score,
'-99' AS v4_overall_model_other_decline_score,
CAST(`pypl-edw`.cw_udf.cw_round_half_even_bignumeric(CAST(mdlscore.scorelabel0
AS numeric), 4) AS numeric) AS v6_overall_model_approval_score,
CAST(`pypl-edw`.cw_udf.cw_round_half_even_bignumeric(CAST(mdlscore.scorelabel1
AS numeric), 4) AS numeric) AS v6_overall_model_nsf_decline_score,
CAST(`pypl-edw`.cw_udf.cw_round_half_even_bignumeric(CAST(mdlscore.scorelabel2
AS numeric), 4) AS numeric) AS v6_overall_model_other_decline_score
FROM
v6 mdlscore
LEFT OUTER JOIN
pypl-edw.pp_cmt_access_views.fact_authrate_card_dtl facd
ON
facd.card_hmac = mdlscore.cc_number_hash
LEFT OUTER JOIN
stmpy_binhmac bin
ON
bin.bin_hmac = facd.bin_hmac;

-- Checking the number of records inserted, raising an error if it's 0


IF @@row_count = 0 THEN
RAISE USING MESSAGE = 'Script terminated because zero records inserted';
END IF;

-- Handling error(s)
EXCEPTION WHEN ERROR THEN
RAISE USING MESSAGE = @@error.message;
-- End of procedural language
END;

You might also like