-- ICA_Bank_Decline_Threshold_Block
DROP TABLE team_kingkong.offus_ICA_Bank_Decline_Threshold_Block_breaches;
-- CREATE TABLE team_kingkong.offus_ICA_Bank_Decline_Threshold_Block_breaches AS
INSERT INTO team_kingkong.offus_ICA_Bank_Decline_Threshold_Block_breaches
with offus_txn as
(SELECT globalcardindex, transactionid, txn_amount, txn_date, paytmmerchantid, txn_timestamp, paymethod
, case when edc_mid is not null then 'EDC' else 'QR' end as mid_type
, isindian, txn_status
FROM
(SELECT DISTINCT pg_mid from cdo.total_offline_merchant_base_snapshot_v3) f
INNER join
(select distinct transactionid
, cast(eventamount as double)/100 as txn_amount
, paytmmerchantid
, globalcardindex
, DATE(dl_last_updated) AS txn_date
, CAST(velocitytimestamp AS DOUBLE) AS txn_timestamp
, paymethod
, isindian
from cdp_risk_transform.maquette_flattened_offus_snapshot_v3
where dl_last_updated BETWEEN DATE(DATE'2025-01-01' - INTERVAL '1' DAY) AND DATE'2025-01-31'
and paymethod in ('CREDIT_CARD','DEBIT_CARD')
AND actionrecommended <> 'BLOCK' AND responsestatus = 'SUCCESS') a
on a.paytmmerchantid = f.pg_mid
LEFT JOIN
(SELECT DISTINCT mid AS edc_mid FROM paytmpgdb.entity_edc_info_snapshot_v3
WHERE terminal_status = 'ACTIVE' AND dl_last_updated >= DATE '2010-01-01') b
ON a.paytmmerchantid = b.edc_mid
INNER JOIN
(select distinct txn_id as pg_txn_id, txn_status
from dwh.pg_olap
where ingest_date BETWEEN DATE'2025-01-01' AND DATE(DATE'2025-01-31' + INTERVAL '1' DAY)
and txn_started_at BETWEEN DATE'2025-01-01' AND DATE(DATE'2025-01-31' + INTERVAL '1' DAY)) d
on a.transactionid = d.pg_txn_id)
SELECT *, CASE
WHEN txn1_hr >= txn1_hr_threshold
AND txn6_hr >= txn6_hr_threshold AND txn_amount > 10000
AND txn24_hr >= txn24_hr_threshold
THEN 'txn1_hr breach, txn6_hr + high txn_amount breach, txn24_hr breach'
WHEN txn1_hr >= txn1_hr_threshold
AND txn6_hr >= txn6_hr_threshold AND txn_amount > 10000
THEN 'txn1_hr breach, txn6_hr + high txn_amount breach'
WHEN txn1_hr >= txn1_hr_threshold
AND txn24_hr >= txn24_hr_threshold
THEN 'txn1_hr breach, txn24_hr breach'
WHEN txn6_hr >= txn6_hr_threshold AND txn_amount > 10000
AND txn24_hr >= txn24_hr_threshold
THEN 'txn6_hr + high txn_amount breach, txn24_hr breach'
WHEN txn1_hr >= txn1_hr_threshold THEN 'txn1_hr breach'
WHEN txn6_hr >= txn6_hr_threshold AND txn_amount > 10000 THEN 'txn6_hr + high txn_amount breach'
WHEN txn24_hr >= txn24_hr_threshold THEN 'txn24_hr breach'
ELSE NULL END AS breach_reason FROM
(SELECT a.globalcardindex, A.transactionid, A.txn_amount, A.txn_date, A.paytmmerchantid, A.txn_timestamp
, A.mid_type, A.paymethod
, COUNT(IF((A.txn_timestamp - B.txn_timestamp) BETWEEN 0 AND 3600000, B.transactionid, NULL)) AS txn1_hr
, 3 txn1_hr_threshold
, COUNT(IF((A.txn_timestamp - B.txn_timestamp) BETWEEN 0 AND 21600000, B.transactionid, NULL)) AS txn6_hr
, 3 txn6_hr_threshold
, COUNT(IF((A.txn_timestamp - B.txn_timestamp) BETWEEN 0 AND 122400000, B.transactionid, NULL)) AS txn24_hr
, 5 AS txn24_hr_threshold
, 'ICA_Bank_Decline_Threshold_Block' AS rule_name
FROM
(SELECT * FROM offus_txn
WHERE txn_date BETWEEN DATE'2025-01-01' AND DATE'2025-01-31'
AND isindian = 'false' and txn_status = 'SUCCESS')A
INNER JOIN
(SELECT * FROM offus_txn
WHERE txn_status = 'CLOSED')B
ON A.globalcardindex = b.globalcardindex AND A.paytmmerchantid = B.paytmmerchantid
AND A.transactionid <> B.transactionid AND A.txn_timestamp > B.txn_timestamp
GROUP BY 1,2,3,4,5,6,7,8)
WHERE (txn1_hr >= txn1_hr_threshold) OR
(txn6_hr >= txn6_hr_threshold AND txn_amount > 10000) OR
(txn24_hr >= txn24_hr_threshold)
;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter