From e040f6942e5f7721e607086bfd53487319ce4985 Mon Sep 17 00:00:00 2001 From: user_client2024 Date: Wed, 9 Oct 2024 03:59:35 +0000 Subject: [PATCH] System save at 09/10/2024 09:29 by user_client2024 --- .ipynb_checkpoints/main-checkpoint.ipynb | 447 +++++++++++++++++++++ main.ipynb | 480 +++++++++++++++++++++-- main.py | 150 +++++++ 3 files changed, 1044 insertions(+), 33 deletions(-) create mode 100644 .ipynb_checkpoints/main-checkpoint.ipynb create mode 100644 main.py diff --git a/.ipynb_checkpoints/main-checkpoint.ipynb b/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..d4445a1 --- /dev/null +++ b/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,447 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2f9a4ca7-c066-4d93-9957-0d9145f9265d", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from tms_data_interface import SQLQueryInterface\n", + "seq = SQLQueryInterface(schema=\"transactionschema\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fc212ace-ca7a-45f2-8137-f436c6123652", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[['account_data_v1'],\n", + " ['account_data_v2'],\n", + " ['alert_data_v1'],\n", + " ['alert_data_v2'],\n", + " ['customer_data_v1'],\n", + " ['customer_data_v2'],\n", + " ['transaction10m'],\n", + " ['transaction60m']]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "seq.execute_raw(\"show tables\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "134d0b3d-5481-4975-af07-c80ab09d6dd2", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "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.Wash_Ratio,\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", + " case\n", + " when subquery.Debit_transaction_amount = 0\n", + " or subquery.Debit_transaction_amount is NULL then 0\n", + " else subquery.Credit_transaction_amount / subquery.Debit_transaction_amount\n", + " end as Wash_Ratio\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} as trans_table left join {acc_data} as 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} as trans_table left join {acc_data} as 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", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d220561a-34c9-48d2-8e2f-5d174a87540b", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "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\",\n", + " \"Total_no_of_credit_transactions\",\n", + " \"Debit_transaction_amount\", \"Total_no_of_debit_transactions\",\n", + " \"Wash_Ratio\", \"Segment\", \"Risk\", \"SAR_FLAG\"]\n", + " df = pd.DataFrame(row_list, columns = cols)\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "2e5a0ea9-64cd-4a8d-9a5d-e5e7b36a401a", + "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", + " \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_transactionsWash_RatioSegmentRiskSAR_FLAG
0PN8086244.601504e+0932394.461280e+0931291.031431Corporate BankingMedium RiskN
1PN6630412.106224e+0915732.281829e+0915630.923042Corporate BankingLow RiskN
2PN5259131.057799e+097761.223876e+098500.864302Whole Sale BankingLow RiskN
3PN4402744.806265e+0935064.972813e+0935990.966508Whole Sale BankingMedium RiskN
4PN2130263.982349e+0928094.122674e+0927830.965963Whole Sale BankingMedium RiskN
..............................
10009PN7747413.373466e+072502.443148e+073811.380787Priority BankingMedium RiskN
10010PN8683263.785344e+072592.408309e+073521.571785Ultra High NetWorthMedium RiskY
10011PN6678373.330357e+072562.676301e+073591.244388Mass MarketMedium RiskN
10012PN8095663.890076e+072762.554121e+074001.523059OthersLow RiskN
10013PN7396473.505184e+072232.232980e+073811.569734OthersLow RiskN
\n", + "

10014 rows × 9 columns

\n", + "
" + ], + "text/plain": [ + " Focal_id Credit_transaction_amount Total_no_of_credit_transactions \\\n", + "0 PN808624 4.601504e+09 3239 \n", + "1 PN663041 2.106224e+09 1573 \n", + "2 PN525913 1.057799e+09 776 \n", + "3 PN440274 4.806265e+09 3506 \n", + "4 PN213026 3.982349e+09 2809 \n", + "... ... ... ... \n", + "10009 PN774741 3.373466e+07 250 \n", + "10010 PN868326 3.785344e+07 259 \n", + "10011 PN667837 3.330357e+07 256 \n", + "10012 PN809566 3.890076e+07 276 \n", + "10013 PN739647 3.505184e+07 223 \n", + "\n", + " Debit_transaction_amount Total_no_of_debit_transactions Wash_Ratio \\\n", + "0 4.461280e+09 3129 1.031431 \n", + "1 2.281829e+09 1563 0.923042 \n", + "2 1.223876e+09 850 0.864302 \n", + "3 4.972813e+09 3599 0.966508 \n", + "4 4.122674e+09 2783 0.965963 \n", + "... ... ... ... \n", + "10009 2.443148e+07 381 1.380787 \n", + "10010 2.408309e+07 352 1.571785 \n", + "10011 2.676301e+07 359 1.244388 \n", + "10012 2.554121e+07 400 1.523059 \n", + "10013 2.232980e+07 381 1.569734 \n", + "\n", + " Segment Risk SAR_FLAG \n", + "0 Corporate Banking Medium Risk N \n", + "1 Corporate Banking Low Risk N \n", + "2 Whole Sale Banking Low Risk N \n", + "3 Whole Sale Banking Medium Risk N \n", + "4 Whole Sale Banking Medium Risk N \n", + "... ... ... ... \n", + "10009 Priority Banking Medium Risk N \n", + "10010 Ultra High NetWorth Medium Risk Y \n", + "10011 Mass Market Medium Risk N \n", + "10012 Others Low Risk N \n", + "10013 Others Low Risk N \n", + "\n", + "[10014 rows x 9 columns]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# sen = Scenario()\n", + "# sen.logic()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "150bb5ce-6be1-44fc-a606-6d375354626d", + "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 +} diff --git a/main.ipynb b/main.ipynb index 3277afb..d4445a1 100644 --- a/main.ipynb +++ b/main.ipynb @@ -1,33 +1,447 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0", - "metadata": {}, - "outputs": [], - "source": "from tms_data_interface import SQLQueryInterface\n\nclass Scenario:\n\tseq = SQLQueryInterface()\n\n\tdef logic(self, **kwargs):\n\t\t# Write your code here\n" - } - ], - "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.8.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} \ No newline at end of file +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2f9a4ca7-c066-4d93-9957-0d9145f9265d", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from tms_data_interface import SQLQueryInterface\n", + "seq = SQLQueryInterface(schema=\"transactionschema\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fc212ace-ca7a-45f2-8137-f436c6123652", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[['account_data_v1'],\n", + " ['account_data_v2'],\n", + " ['alert_data_v1'],\n", + " ['alert_data_v2'],\n", + " ['customer_data_v1'],\n", + " ['customer_data_v2'],\n", + " ['transaction10m'],\n", + " ['transaction60m']]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "seq.execute_raw(\"show tables\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "134d0b3d-5481-4975-af07-c80ab09d6dd2", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "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.Wash_Ratio,\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", + " case\n", + " when subquery.Debit_transaction_amount = 0\n", + " or subquery.Debit_transaction_amount is NULL then 0\n", + " else subquery.Credit_transaction_amount / subquery.Debit_transaction_amount\n", + " end as Wash_Ratio\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} as trans_table left join {acc_data} as 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} as trans_table left join {acc_data} as 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", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d220561a-34c9-48d2-8e2f-5d174a87540b", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "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\",\n", + " \"Total_no_of_credit_transactions\",\n", + " \"Debit_transaction_amount\", \"Total_no_of_debit_transactions\",\n", + " \"Wash_Ratio\", \"Segment\", \"Risk\", \"SAR_FLAG\"]\n", + " df = pd.DataFrame(row_list, columns = cols)\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "2e5a0ea9-64cd-4a8d-9a5d-e5e7b36a401a", + "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", + " \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_transactionsWash_RatioSegmentRiskSAR_FLAG
0PN8086244.601504e+0932394.461280e+0931291.031431Corporate BankingMedium RiskN
1PN6630412.106224e+0915732.281829e+0915630.923042Corporate BankingLow RiskN
2PN5259131.057799e+097761.223876e+098500.864302Whole Sale BankingLow RiskN
3PN4402744.806265e+0935064.972813e+0935990.966508Whole Sale BankingMedium RiskN
4PN2130263.982349e+0928094.122674e+0927830.965963Whole Sale BankingMedium RiskN
..............................
10009PN7747413.373466e+072502.443148e+073811.380787Priority BankingMedium RiskN
10010PN8683263.785344e+072592.408309e+073521.571785Ultra High NetWorthMedium RiskY
10011PN6678373.330357e+072562.676301e+073591.244388Mass MarketMedium RiskN
10012PN8095663.890076e+072762.554121e+074001.523059OthersLow RiskN
10013PN7396473.505184e+072232.232980e+073811.569734OthersLow RiskN
\n", + "

10014 rows × 9 columns

\n", + "
" + ], + "text/plain": [ + " Focal_id Credit_transaction_amount Total_no_of_credit_transactions \\\n", + "0 PN808624 4.601504e+09 3239 \n", + "1 PN663041 2.106224e+09 1573 \n", + "2 PN525913 1.057799e+09 776 \n", + "3 PN440274 4.806265e+09 3506 \n", + "4 PN213026 3.982349e+09 2809 \n", + "... ... ... ... \n", + "10009 PN774741 3.373466e+07 250 \n", + "10010 PN868326 3.785344e+07 259 \n", + "10011 PN667837 3.330357e+07 256 \n", + "10012 PN809566 3.890076e+07 276 \n", + "10013 PN739647 3.505184e+07 223 \n", + "\n", + " Debit_transaction_amount Total_no_of_debit_transactions Wash_Ratio \\\n", + "0 4.461280e+09 3129 1.031431 \n", + "1 2.281829e+09 1563 0.923042 \n", + "2 1.223876e+09 850 0.864302 \n", + "3 4.972813e+09 3599 0.966508 \n", + "4 4.122674e+09 2783 0.965963 \n", + "... ... ... ... \n", + "10009 2.443148e+07 381 1.380787 \n", + "10010 2.408309e+07 352 1.571785 \n", + "10011 2.676301e+07 359 1.244388 \n", + "10012 2.554121e+07 400 1.523059 \n", + "10013 2.232980e+07 381 1.569734 \n", + "\n", + " Segment Risk SAR_FLAG \n", + "0 Corporate Banking Medium Risk N \n", + "1 Corporate Banking Low Risk N \n", + "2 Whole Sale Banking Low Risk N \n", + "3 Whole Sale Banking Medium Risk N \n", + "4 Whole Sale Banking Medium Risk N \n", + "... ... ... ... \n", + "10009 Priority Banking Medium Risk N \n", + "10010 Ultra High NetWorth Medium Risk Y \n", + "10011 Mass Market Medium Risk N \n", + "10012 Others Low Risk N \n", + "10013 Others Low Risk N \n", + "\n", + "[10014 rows x 9 columns]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# sen = Scenario()\n", + "# sen.logic()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "150bb5ce-6be1-44fc-a606-6d375354626d", + "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 +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..2f5e9af --- /dev/null +++ b/main.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[1]: + + +import pandas as pd + + +# In[5]: + + +from tms_data_interface import SQLQueryInterface +seq = SQLQueryInterface(schema="transactionschema") + + +# In[6]: + + +seq.execute_raw("show tables") + + +# In[7]: + + +query = """ + select final.CUSTOMER_NUMBER_main as Focal_id, + final.Credit_transaction_amount, + final.Total_no_of_credit_transactions, + final.Debit_transaction_amount, + final.Total_no_of_debit_transactions, + final.Wash_Ratio, + final.SEGMENT, + final.RISK, + final.SAR_FLAG + from + ( + ( + select subquery.CUSTOMER_NUMBER_1 as CUSTOMER_NUMBER_main, + subquery.Credit_transaction_amount, + subquery.Total_no_of_credit_transactions, + case + when subquery.Debit_transaction_amount is NULL then 0 + else Debit_transaction_amount + end as Debit_transaction_amount, + case + when subquery.Total_no_of_debit_transactions is NULL then 0 + else Total_no_of_debit_transactions + end as Total_no_of_debit_transactions, + case + when subquery.Debit_transaction_amount = 0 + or subquery.Debit_transaction_amount is NULL then 0 + else subquery.Credit_transaction_amount / subquery.Debit_transaction_amount + end as Wash_Ratio + from + ( + ( + select customer_number as CUSTOMER_NUMBER_1, + sum(transaction_amount) as Credit_transaction_amount, + count(*) as Total_no_of_credit_transactions + from + ( + select * + from {trans_data} as trans_table left join {acc_data} as acc_table + on trans_table.benef_account_number = acc_table.account_number + ) + where account_number not in ('None') + group by 1 + ) credit left join + ( + select customer_number as CUSTOMER_NUMBER_2, + sum(transaction_amount) as Debit_transaction_amount, + count(*) as Total_no_of_debit_transactions + from + ( + select * + from {trans_data} as trans_table left join {acc_data} as acc_table + on trans_table.orig_account_number = acc_table.account_number + ) + where account_number not in ('None') + group by 1 + ) debit on credit.CUSTOMER_NUMBER_1 = debit.CUSTOMER_NUMBER_2 + ) subquery + ) main left join + ( + select subquery.CUSTOMER_NUMBER_3 as CUSTOMER_NUMBER_cust, + subquery.SEGMENT, + subquery.RISK, + case + when subquery.SAR_FLAG is NULL then 'N' + else subquery.SAR_FLAG + end as SAR_FLAG + from + ( + ( + select customer_number as CUSTOMER_NUMBER_3, + business_segment as SEGMENT, + case + when RISK_CLASSIFICATION = 1 then 'Low Risk' + when RISK_CLASSIFICATION = 2 then 'Medium Risk' + when RISK_CLASSIFICATION = 3 then 'High Risk' + else 'Unknown Risk' + end AS RISK + from {cust_data} + ) cd left join + ( + select customer_number as CUSTOMER_NUMBER_4, + sar_flag as SAR_FLAG + from {alert_data} + ) ad on cd.CUSTOMER_NUMBER_3 = ad.CUSTOMER_NUMBER_4 + ) subquery + ) cust_alert on cust_alert.CUSTOMER_NUMBER_cust = main.CUSTOMER_NUMBER_main + ) final +""" + + +# In[8]: + + +from tms_data_interface import SQLQueryInterface + +class Scenario: + seq = SQLQueryInterface(schema="transactionschema") + + def logic(self, **kwargs): + row_list = self.seq.execute_raw(query.format(trans_data="transaction10m", + cust_data="customer_data_v1", + acc_data="account_data_v1", + alert_data="alert_data_v1") + ) + cols = ["Focal_id", "Credit_transaction_amount", + "Total_no_of_credit_transactions", + "Debit_transaction_amount", "Total_no_of_debit_transactions", + "Wash_Ratio", "Segment", "Risk", "SAR_FLAG"] + df = pd.DataFrame(row_list, columns = cols) + return df + + +# In[9]: + + +# sen = Scenario() +# sen.logic() + + +# In[ ]: + + + +