-- WEEKLY LIMIT ON EGV PURCHASE AMT : RULE 3 ROLLING SELECT '2022' AS year, '5' as month, 'HML_DISTINCT_MERCHANT_PAYMENT_WALLET_TXN_LIMIT' as rule_name, 'LIVE' AS status, 'MIN + FULL' as kyc, P.catg, 'Hourly' as Frequency, P.breaches FROM (SELECT Z.catg, COUNT(DISTINCT Z.transaction_id) AS breaches FROM (SELECT A.senderuserid, A.transaction_id, SUM(B.totaltransactionamount) AS totAmt, IF(C.hml_category IS NOT NULL, C.hml_category, 'LOW RISK') AS catg, CASE WHEN C.hml_category = 'HIGH RISK' THEN 40000 WHEN C.hml_category = 'MEDIUM RISK' THEN 80000 ELSE 80000 END AS limit FROM (SELECT senderuserid, updated_date as txnTime, transaction_id, totaltransactionamount from fraud.transaction_details where year(updated_date) = 2022 and month(updated_date) = 5 and transaction_details.sendertype = 'INTERNAL_USER' and transaction_details.pay_transaction_status = 'COMPLETED' and transaction_details.receiveruser = 'PHONEPEGC')A LEFT JOIN (SELECT senderuserid, updated_date as txnTime, transaction_id, totaltransactionamount from fraud.transaction_details where year(updated_date) = 2022 and month(updated_date) = 5 and transaction_details.sendertype = 'INTERNAL_USER' and transaction_details.pay_transaction_status = 'COMPLETED' and transaction_details.receiveruser = 'PHONEPEGC')B ON A.senderuserid = B.senderuserid AND ((UNIX_TIMESTAMP(A.txnTime) - UNIX_TIMESTAMP(B.txnTime))/604800) BETWEEN 0 AND 1 AND A.txnTime > B.txnTime LEFT JOIN (SELECT DISTINCT senderuserid, hml_category FROM fraud.hml_classification)C ON A.senderuserid = C.senderuserid GROUP BY A.senderuserid, A.transaction_id, C.hml_category)Z WHERE Z.totAmt > Z.limit GROUP BY Z.catg)P