{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0", "metadata": { "tags": [] }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "query = \"\"\"\n", " select final.CUSTOMER_NUMBER_main as Focal_id,\n", " final.Credit_transaction_amount,\n", " final.Total_no_of_credit_transactions,\n", " final.Debit_transaction_amount,\n", " final.Total_no_of_debit_transactions,\n", " final.SEGMENT,\n", " final.RISK,\n", " final.SAR_FLAG\n", " from \n", " (\n", " (\n", " select subquery.CUSTOMER_NUMBER_1 as CUSTOMER_NUMBER_main,\n", " subquery.Credit_transaction_amount,\n", " subquery.Total_no_of_credit_transactions,\n", " case\n", " when subquery.Debit_transaction_amount is NULL then 0\n", " else Debit_transaction_amount\n", " end as Debit_transaction_amount,\n", " case\n", " when subquery.Total_no_of_debit_transactions is NULL then 0\n", " else Total_no_of_debit_transactions\n", " end as Total_no_of_debit_transactions\n", " from \n", " (\n", " (\n", " select customer_number as CUSTOMER_NUMBER_1, \n", " sum(transaction_amount) as Credit_transaction_amount, \n", " count(*) as Total_no_of_credit_transactions\n", " from \n", " (\n", " select * \n", " from {trans_data} trans_table left join {acc_data} acc_table\n", " on trans_table.benef_account_number = acc_table.account_number\n", " )\n", " where account_number not in ('None')\n", " group by 1\n", " ) credit left join\n", " (\n", " select customer_number as CUSTOMER_NUMBER_2, \n", " sum(transaction_amount) as Debit_transaction_amount, \n", " count(*) as Total_no_of_debit_transactions\n", " from \n", " (\n", " select * \n", " from {trans_data} trans_table left join {acc_data} acc_table\n", " on trans_table.orig_account_number = acc_table.account_number\n", " )\n", " where account_number not in ('None')\n", " group by 1\n", " ) debit on credit.CUSTOMER_NUMBER_1 = debit.CUSTOMER_NUMBER_2 \n", " ) subquery\n", " ) main left join \n", " (\n", " select subquery.CUSTOMER_NUMBER_3 as CUSTOMER_NUMBER_cust,\n", " subquery.SEGMENT,\n", " subquery.RISK,\n", " case\n", " when subquery.SAR_FLAG is NULL then 'N'\n", " else subquery.SAR_FLAG\n", " end as SAR_FLAG \n", " from\n", " (\n", " (\n", " select customer_number as CUSTOMER_NUMBER_3, \n", " business_segment as SEGMENT,\n", " case\n", " when RISK_CLASSIFICATION = 1 then 'Low Risk'\n", " when RISK_CLASSIFICATION = 2 then 'Medium Risk'\n", " when RISK_CLASSIFICATION = 3 then 'High Risk'\n", " else 'Unknown Risk'\n", " end AS RISK\n", " from {cust_data}\n", " ) cd left join\n", " (\n", " select customer_number as CUSTOMER_NUMBER_4, \n", " sar_flag as SAR_FLAG\n", " from {alert_data}\n", " ) ad on cd.CUSTOMER_NUMBER_3 = ad.CUSTOMER_NUMBER_4\n", " ) subquery\n", " ) cust_alert on cust_alert.CUSTOMER_NUMBER_cust = main.CUSTOMER_NUMBER_main\n", " ) final\n", "\"\"\"\n", "\n", "from tms_data_interface import SQLQueryInterface\n", " \n", "class Scenario:\n", " seq = SQLQueryInterface(schema=\"transactionschema\")\n", " \n", " def logic(self, **kwargs):\n", " row_list = self.seq.execute_raw(query.format(trans_data=\"transaction10m\",\n", " cust_data=\"customer_data_v1\",\n", " acc_data=\"account_data_v1\",\n", " alert_data=\"alert_data_v1\")\n", " )\n", " cols = [\"Focal_id\", \"Credit_transaction_amount\", \"Total_no_of_credit_transactions\",\n", " \"Debit_transaction_amount\", \"Total_no_of_debit_transactions\",\n", " \"Segment\", \"Risk\", \"SAR_FLAG\"]\n", " df = pd.DataFrame(row_list, columns = cols)\n", " return df" ] }, { "cell_type": "code", "execution_count": 3, "id": "741546f6-df8f-4578-bbd2-79aa38a98c5b", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Focal_idCredit_transaction_amountTotal_no_of_credit_transactionsDebit_transaction_amountTotal_no_of_debit_transactionsSegmentRiskSAR_FLAG
0PN1821211.644268e+0911681.717572e+091198SMELow RiskN
1PN4762082.792031e+0919932.777591e+092010Whole Sale BankingLow RiskN
2PN4139853.621153e+0924383.290559e+092375SMEHigh RiskN
3PN9061301.666937e+0912221.756952e+091177Whole Sale BankingLow RiskN
4PN4469143.379522e+0923783.512353e+092454SMELow RiskN
...........................
10009PN7232074.765892e+072982.651826e+07378Ultra High NetWorthLow RiskN
10010PN8607733.537672e+072562.578618e+07363OthersLow RiskN
10011PN7418433.787797e+072552.909541e+07346Priority BankingMedium RiskN
10012PN5883933.567320e+072542.676294e+07391Mass MarketLow RiskN
10013PN1683723.166679e+072532.707380e+07355Mass MarketLow RiskN
\n", "

10014 rows × 8 columns

\n", "
" ], "text/plain": [ " Focal_id Credit_transaction_amount Total_no_of_credit_transactions \\\n", "0 PN182121 1.644268e+09 1168 \n", "1 PN476208 2.792031e+09 1993 \n", "2 PN413985 3.621153e+09 2438 \n", "3 PN906130 1.666937e+09 1222 \n", "4 PN446914 3.379522e+09 2378 \n", "... ... ... ... \n", "10009 PN723207 4.765892e+07 298 \n", "10010 PN860773 3.537672e+07 256 \n", "10011 PN741843 3.787797e+07 255 \n", "10012 PN588393 3.567320e+07 254 \n", "10013 PN168372 3.166679e+07 253 \n", "\n", " Debit_transaction_amount Total_no_of_debit_transactions \\\n", "0 1.717572e+09 1198 \n", "1 2.777591e+09 2010 \n", "2 3.290559e+09 2375 \n", "3 1.756952e+09 1177 \n", "4 3.512353e+09 2454 \n", "... ... ... \n", "10009 2.651826e+07 378 \n", "10010 2.578618e+07 363 \n", "10011 2.909541e+07 346 \n", "10012 2.676294e+07 391 \n", "10013 2.707380e+07 355 \n", "\n", " Segment Risk SAR_FLAG \n", "0 SME Low Risk N \n", "1 Whole Sale Banking Low Risk N \n", "2 SME High Risk N \n", "3 Whole Sale Banking Low Risk N \n", "4 SME Low Risk N \n", "... ... ... ... \n", "10009 Ultra High NetWorth Low Risk N \n", "10010 Others Low Risk N \n", "10011 Priority Banking Medium Risk N \n", "10012 Mass Market Low Risk N \n", "10013 Mass Market Low Risk N \n", "\n", "[10014 rows x 8 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sen = Scenario()\n", "# sen.logic()" ] }, { "cell_type": "code", "execution_count": null, "id": "2f8eee99-046e-4d56-afb8-9007fdc2f5b2", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }