%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.txns >= Z.limit, 1, 0)) AS breaches , 'egvGiftTxnCntHourly' as Rule -- , 'egvGiftTxnCntDaily' as Rule -- , 'egvGiftTxnCntWeekly' as Rule FROM (SELECT A.user_id, A.txn_id, month(A.txnTime) as month, COUNT(DISTINCT b.txn_id) as txns, IF(C.hml_category IS NOT NULL, C.hml_category, 'LOW RISK') AS risk_catg, CASE WHEN C.hml_category = 'HIGH RISK' THEN 1 WHEN C.hml_category = 'MEDIUM RISK' THEN 2 ELSE 3 END AS limit -- CASE WHEN C.hml_category = 'HIGH RISK' THEN 3 WHEN C.hml_category = 'MEDIUM RISK' THEN 4 ELSE 6 END AS limit -- 8 AS limit FROM (SELECT DISTINCT purchasing_user_id as user_id, phonepe_transaction_id AS txn_id, 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, 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)Z GROUP BY Z.month, Z.risk_catg