Preview:
DROP TABLE team_kingkong.offus_edc_card_velocity_count_breaches;

-- CREATE TABLE team_kingkong.offus_edc_card_velocity_count_breaches AS
INSERT INTO team_kingkong.offus_edc_card_velocity_count_breaches
with offus_txn as
(SELECT DISTINCT globalcardindex, transactionid, txn_amount, txn_date, paytmmerchantid, txn_timestamp FROM
    (SELECT DISTINCT pg_mid from cdo.total_offline_merchant_base_snapshot_v3) f
INNER join
    (select 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
    from cdp_risk_transform.maquette_flattened_offus_snapshot_v3
    where dl_last_updated BETWEEN DATE'2025-01-01' AND DATE'2025-01-31'
    and paymethod in ('CREDIT_CARD','DEBIT_CARD','EMI','EMI_DC')
    AND actionrecommended <> 'BLOCK'AND responsestatus = 'SUCCESS'
    AND globalcardindex IS NOT NULL AND globalcardindex <> '' AND globalcardindex <> ' ' 
    AND paytmmerchantid IS NOT NULL AND paytmmerchantid <> '' AND paytmmerchantid <> ' ') a
on a.paytmmerchantid = f.pg_mid
LEFT JOIN
    (SELECT 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
    from dwh.pg_olap
    where ingest_date BETWEEN DATE'2025-01-01' AND DATE'2025-01-31' 
    and txn_started_at BETWEEN  DATE'2025-01-01' AND DATE'2025-01-31'
    and txn_status = 'SUCCESS') d
on a.transactionid = d.pg_txn_id)

SELECT *, CASE
  WHEN txn1_min >= txn1_min_threshold 
       AND txn6_hr >= txn6_hr_threshold 
       AND txn24_hr >= txn24_hr_threshold THEN 'txn1_min, txn6_hr, txn24_hr breach'
  WHEN txn1_min >= txn1_min_threshold 
       AND txn6_hr >= txn6_hr_threshold THEN 'txn1_min, txn6_hr breach'
  WHEN txn1_min >= txn1_min_threshold 
       AND txn24_hr >= txn24_hr_threshold THEN 'txn1_min, txn24_hr breach'
  WHEN txn6_hr >= txn6_hr_threshold 
       AND txn24_hr >= txn24_hr_threshold THEN 'txn6_hr, txn24_hr breach'
  WHEN txn1_min >= txn1_min_threshold THEN 'txn1_min breach'
  WHEN txn6_hr >= txn6_hr_threshold THEN 'txn6_hr 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, 'edc_card_velocity_count' AS rule_name, A.txn_timestamp
    , COUNT(DISTINCT IF((A.txn_timestamp - B.txn_timestamp) BETWEEN 0 AND 60000, B.transactionid, NULL)) AS txn1_min
    , 2 AS txn1_min_threshold
    , COUNT(DISTINCT IF((A.txn_timestamp - B.txn_timestamp) BETWEEN 0 AND 21600000, B.transactionid, NULL)) AS txn6_hr
    , 5 as txn6_hr_threshold
    , COUNT(DISTINCT B.transactionid) as txn24_hr
    , 10 AS txn24_hr_threshold
    FROM
        (SELECT * FROM offus_txn
        WHERE txn_date BETWEEN DATE'2025-01-01' AND DATE'2025-01-31')A
    INNER JOIN
        (SELECT * FROM offus_txn)B
    ON A.globalcardindex = b.globalcardindex AND A.paytmmerchantid = B.paytmmerchantid AND (A.txn_timestamp - B.txn_timestamp) BETWEEN 0 AND 86400000 -- <= 1d
    AND A.transactionid <> B.transactionid
    GROUP BY 1,2,3,4,5,6,7)
WHERE (txn1_min >= txn1_min_threshold) OR (txn6_hr >= txn6_hr_threshold) OR (txn24_hr >= txn24_hr_threshold);
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