diff --git a/.ipynb_checkpoints/main-checkpoint.ipynb b/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..1c3643e --- /dev/null +++ b/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,307 @@ +{ + "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", + " CAST(final.Cash_deposit_total AS DECIMAL(18, 2)) AS Cash_deposit_total,\n", + " final.Cash_deposit_count,\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.Cash_deposit_total,\n", + " subquery.Cash_deposit_count\n", + " from \n", + " (\n", + " select customer_number as CUSTOMER_NUMBER_1, \n", + " sum(transaction_amount) as Cash_deposit_total, \n", + " count(*) as Cash_deposit_count\n", + " from \n", + " (\n", + " select * \n", + " from {trans_data} trans_table \n", + " left join {acc_data} acc_table\n", + " on trans_table.benef_account_number = acc_table.account_number\n", + " ) trans\n", + " where account_number not in ('None')\n", + " and transaction_desc = 'CASH RELATED TRANSACTION'\n", + " group by customer_number\n", + " ) subquery\n", + " ) main \n", + " left join \n", + " (\n", + " select cd.CUSTOMER_NUMBER_3 as CUSTOMER_NUMBER_cust,\n", + " cd.SEGMENT,\n", + " cd.RISK,\n", + " case\n", + " when ad.SAR_FLAG is NULL then 'N'\n", + " else ad.SAR_FLAG\n", + " end as SAR_FLAG \n", + " from\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 \n", + " left join\n", + " (\n", + " select customer_number as CUSTOMER_NUMBER_4, \n", + " sar_flag as SAR_FLAG\n", + " from {alert_data}\n", + " ) ad \n", + " on cd.CUSTOMER_NUMBER_3 = ad.CUSTOMER_NUMBER_4\n", + " ) as cust_alert\n", + " on cust_alert.CUSTOMER_NUMBER_cust = main.CUSTOMER_NUMBER_main\n", + ") as 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\", \"Cash_deposit_total\", \"Cash_deposit_count\",\n", + " \"Segment\", \"Risk\", \"SAR_FLAG\"]\n", + " df = pd.DataFrame(row_list, columns = cols)\n", + " df[\"Cash_deposit_total\"] = df[\"Cash_deposit_total\"].astype(float)\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1f20337b-8116-47e5-8743-1ba41e2df819", + "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", + "
Focal_idCash_deposit_totalCash_deposit_countSegmentRiskSAR_FLAG
0PN4693048049952.0141Mass MarketHigh RiskN
1PN6057439766101.0175OthersLow RiskN
2PN3992158469943.0164Mass MarketLow RiskN
3PN17502017724959.0313OthersLow RiskN
4PN3450857518628.0124OthersMedium RiskN
.....................
9005PN6603298528178.0141OthersLow RiskN
9006PN3368338475036.0150OthersLow RiskN
9007PN4285347871170.0129Priority BankingLow RiskN
9008PN1016608450309.0142Mass MarketLow RiskN
9009PN1986418690226.0166OthersLow RiskN
\n", + "

9010 rows × 6 columns

\n", + "
" + ], + "text/plain": [ + " Focal_id Cash_deposit_total Cash_deposit_count Segment \\\n", + "0 PN469304 8049952.0 141 Mass Market \n", + "1 PN605743 9766101.0 175 Others \n", + "2 PN399215 8469943.0 164 Mass Market \n", + "3 PN175020 17724959.0 313 Others \n", + "4 PN345085 7518628.0 124 Others \n", + "... ... ... ... ... \n", + "9005 PN660329 8528178.0 141 Others \n", + "9006 PN336833 8475036.0 150 Others \n", + "9007 PN428534 7871170.0 129 Priority Banking \n", + "9008 PN101660 8450309.0 142 Mass Market \n", + "9009 PN198641 8690226.0 166 Others \n", + "\n", + " Risk SAR_FLAG \n", + "0 High Risk N \n", + "1 Low Risk N \n", + "2 Low Risk N \n", + "3 Low Risk N \n", + "4 Medium Risk N \n", + "... ... ... \n", + "9005 Low Risk N \n", + "9006 Low Risk N \n", + "9007 Low Risk N \n", + "9008 Low Risk N \n", + "9009 Low Risk N \n", + "\n", + "[9010 rows x 6 columns]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# sen = Scenario()\n", + "# sen.logic()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6de62b37-00d1-4c88-b27b-9a70e05add91", + "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..1c3643e 100644 --- a/main.ipynb +++ b/main.ipynb @@ -1,33 +1,307 @@ -{ - "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": 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", + " CAST(final.Cash_deposit_total AS DECIMAL(18, 2)) AS Cash_deposit_total,\n", + " final.Cash_deposit_count,\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.Cash_deposit_total,\n", + " subquery.Cash_deposit_count\n", + " from \n", + " (\n", + " select customer_number as CUSTOMER_NUMBER_1, \n", + " sum(transaction_amount) as Cash_deposit_total, \n", + " count(*) as Cash_deposit_count\n", + " from \n", + " (\n", + " select * \n", + " from {trans_data} trans_table \n", + " left join {acc_data} acc_table\n", + " on trans_table.benef_account_number = acc_table.account_number\n", + " ) trans\n", + " where account_number not in ('None')\n", + " and transaction_desc = 'CASH RELATED TRANSACTION'\n", + " group by customer_number\n", + " ) subquery\n", + " ) main \n", + " left join \n", + " (\n", + " select cd.CUSTOMER_NUMBER_3 as CUSTOMER_NUMBER_cust,\n", + " cd.SEGMENT,\n", + " cd.RISK,\n", + " case\n", + " when ad.SAR_FLAG is NULL then 'N'\n", + " else ad.SAR_FLAG\n", + " end as SAR_FLAG \n", + " from\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 \n", + " left join\n", + " (\n", + " select customer_number as CUSTOMER_NUMBER_4, \n", + " sar_flag as SAR_FLAG\n", + " from {alert_data}\n", + " ) ad \n", + " on cd.CUSTOMER_NUMBER_3 = ad.CUSTOMER_NUMBER_4\n", + " ) as cust_alert\n", + " on cust_alert.CUSTOMER_NUMBER_cust = main.CUSTOMER_NUMBER_main\n", + ") as 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\", \"Cash_deposit_total\", \"Cash_deposit_count\",\n", + " \"Segment\", \"Risk\", \"SAR_FLAG\"]\n", + " df = pd.DataFrame(row_list, columns = cols)\n", + " df[\"Cash_deposit_total\"] = df[\"Cash_deposit_total\"].astype(float)\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1f20337b-8116-47e5-8743-1ba41e2df819", + "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", + "
Focal_idCash_deposit_totalCash_deposit_countSegmentRiskSAR_FLAG
0PN4693048049952.0141Mass MarketHigh RiskN
1PN6057439766101.0175OthersLow RiskN
2PN3992158469943.0164Mass MarketLow RiskN
3PN17502017724959.0313OthersLow RiskN
4PN3450857518628.0124OthersMedium RiskN
.....................
9005PN6603298528178.0141OthersLow RiskN
9006PN3368338475036.0150OthersLow RiskN
9007PN4285347871170.0129Priority BankingLow RiskN
9008PN1016608450309.0142Mass MarketLow RiskN
9009PN1986418690226.0166OthersLow RiskN
\n", + "

9010 rows × 6 columns

\n", + "
" + ], + "text/plain": [ + " Focal_id Cash_deposit_total Cash_deposit_count Segment \\\n", + "0 PN469304 8049952.0 141 Mass Market \n", + "1 PN605743 9766101.0 175 Others \n", + "2 PN399215 8469943.0 164 Mass Market \n", + "3 PN175020 17724959.0 313 Others \n", + "4 PN345085 7518628.0 124 Others \n", + "... ... ... ... ... \n", + "9005 PN660329 8528178.0 141 Others \n", + "9006 PN336833 8475036.0 150 Others \n", + "9007 PN428534 7871170.0 129 Priority Banking \n", + "9008 PN101660 8450309.0 142 Mass Market \n", + "9009 PN198641 8690226.0 166 Others \n", + "\n", + " Risk SAR_FLAG \n", + "0 High Risk N \n", + "1 Low Risk N \n", + "2 Low Risk N \n", + "3 Low Risk N \n", + "4 Medium Risk N \n", + "... ... ... \n", + "9005 Low Risk N \n", + "9006 Low Risk N \n", + "9007 Low Risk N \n", + "9008 Low Risk N \n", + "9009 Low Risk N \n", + "\n", + "[9010 rows x 6 columns]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# sen = Scenario()\n", + "# sen.logic()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6de62b37-00d1-4c88-b27b-9a70e05add91", + "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..121063e --- /dev/null +++ b/main.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[2]: + + +import pandas as pd + +query = """ + select final.CUSTOMER_NUMBER_main as Focal_id, + CAST(final.Cash_deposit_total AS DECIMAL(18, 2)) AS Cash_deposit_total, + final.Cash_deposit_count, + final.SEGMENT, + final.RISK, + final.SAR_FLAG +from +( + ( + select subquery.CUSTOMER_NUMBER_1 as CUSTOMER_NUMBER_main, + subquery.Cash_deposit_total, + subquery.Cash_deposit_count + from + ( + select customer_number as CUSTOMER_NUMBER_1, + sum(transaction_amount) as Cash_deposit_total, + count(*) as Cash_deposit_count + from + ( + select * + from {trans_data} trans_table + left join {acc_data} acc_table + on trans_table.benef_account_number = acc_table.account_number + ) trans + where account_number not in ('None') + and transaction_desc = 'CASH RELATED TRANSACTION' + group by customer_number + ) subquery + ) main + left join + ( + select cd.CUSTOMER_NUMBER_3 as CUSTOMER_NUMBER_cust, + cd.SEGMENT, + cd.RISK, + case + when ad.SAR_FLAG is NULL then 'N' + else ad.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 + ) as cust_alert + on cust_alert.CUSTOMER_NUMBER_cust = main.CUSTOMER_NUMBER_main +) as final +""" + +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", "Cash_deposit_total", "Cash_deposit_count", + "Segment", "Risk", "SAR_FLAG"] + df = pd.DataFrame(row_list, columns = cols) + df["Cash_deposit_total"] = df["Cash_deposit_total"].astype(float) + return df + + +# In[3]: + + +# sen = Scenario() +# sen.logic() + + +# In[ ]: + + + +