EGV GIFT TXN AMT HOURLY/DAILY/WEEKLY

PHOTO EMBED

Tue Nov 29 2022 09:25:18 GMT+0000 (Coordinated Universal Time)

Saved by @shubhangi_burle

%jdbc(hive)
set tez.queue.name=default;
set hive.execution.engine=tez;

SELECT '2022' AS year, Z.month, 'MIN + FULL' as kyc, Z.risk_catg
-- , 'Hourly' as Frequency
, 'Daily' as Frequency
-- , 'Weekly' as Frequency
, SUM(IF(Z.amt > Z.limit, 1, 0)) AS breaches
-- , 'egvGiftTxnAmtHourly' as Rule
, 'egvGiftTxnAmtDaily' as Rule
-- , 'egvGiftTxnAmtWeekly' as Rule
FROM
    (SELECT A.user_id, A.txn_id, month(A.txnTime) as month, (A.amt + SUM(B.amt)) as amt,
    IF(C.hml_category IS NOT NULL, C.hml_category, 'LOW RISK') AS risk_catg, 
    -- CASE WHEN C.hml_category = 'HIGH RISK' THEN 15000 WHEN C.hml_category = 'MEDIUM RISK' THEN 20000 ELSE 30000 END AS limit
    CASE WHEN C.hml_category = 'HIGH RISK' THEN 40000 WHEN C.hml_category = 'MEDIUM RISK' THEN 40000 ELSE 80000 END AS limit
    -- CASE WHEN C.hml_category = 'HIGH RISK' THEN 100000 WHEN C.hml_category = 'MEDIUM RISK' THEN 100000 ELSE 200000 END AS limit
    FROM
        (SELECT DISTINCT purchasing_user_id as user_id, phonepe_transaction_id AS txn_id, original_balance/100 as amt, created_at as txnTime
        FROM egv.gift_cards
        WHERE year = 2022 AND month = 11 AND gift_card_status NOT IN ('BLOCKED', 'CANCELLED') AND card_link_state = 'LINKED')A
    INNER JOIN
        (SELECT DISTINCT purchasing_user_id as user_id, phonepe_transaction_id AS txn_id, original_balance/100 as amt, created_at as txnTime
        FROM egv.gift_cards
        WHERE year = 2022 AND month = 11 AND gift_card_status NOT IN ('BLOCKED', 'CANCELLED') AND card_link_state = 'LINKED')B
    ON A.user_id = B.user_id AND A.txn_id <> B.txn_id 
    -- AND ((UNIX_TIMESTAMP(A.txnTime) - UNIX_TIMESTAMP(B.txnTime))/3600) BETWEEN 0 AND 1
    AND ((UNIX_TIMESTAMP(A.txnTime) - UNIX_TIMESTAMP(B.txnTime))/86400) BETWEEN 0 AND 1
    -- AND ((UNIX_TIMESTAMP(A.txnTime) - UNIX_TIMESTAMP(B.txnTime))/604800) BETWEEN 0 AND 1
    LEFT JOIN
        (SELECT DISTINCT senderuserid, hml_category FROM fraud.hml_classification)C
    ON A.user_id = C.senderuserid
    GROUP BY A.user_id,  A.txn_id, month(A.txnTime), C.hml_category, A.amt)Z
GROUP BY Z.month, Z.risk_catg
content_copyCOPY