generated from dhairya/scenario_template
System save at 09/10/2024 08:49 by user_client2024
This commit is contained in:
parent
2d4f5399d7
commit
e84566cd33
368
.ipynb_checkpoints/main-checkpoint.ipynb
Normal file
368
.ipynb_checkpoints/main-checkpoint.ipynb
Normal file
@ -0,0 +1,368 @@
|
|||||||
|
{
|
||||||
|
"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": 3,
|
||||||
|
"id": "2edb58c0-33a2-4436-8128-05645af9990d",
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from tms_data_interface import SQLQueryInterface\n",
|
||||||
|
"seq = SQLQueryInterface(schema=\"transactionschema\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "0ccc875a-6ff5-4741-9495-93c6871b1027",
|
||||||
|
"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": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"seq.execute_raw(\"show tables\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "01502887-b11f-46d2-9a52-e493df19d049",
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"query = \"\"\"\n",
|
||||||
|
" select final.CUSTOMER_NUMBER_main as Focal_id,\n",
|
||||||
|
" CAST(final.Total_hrc_transaction_amount AS DECIMAL(18, 2)) AS Total_hrc_transaction_amount,\n",
|
||||||
|
" final.Unique_country_codes,\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.Total_hrc_transaction_amount,\n",
|
||||||
|
" subquery.Unique_country_codes\n",
|
||||||
|
" from \n",
|
||||||
|
" (\n",
|
||||||
|
" select customer_number as CUSTOMER_NUMBER_1, \n",
|
||||||
|
" sum(transaction_amount) as Total_hrc_transaction_amount, \n",
|
||||||
|
" array_join(array_agg(DISTINCT benef_cntry_code), ',') AS unique_country_codes\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",
|
||||||
|
" 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",
|
||||||
|
"\"\"\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "c28e15b5-4b09-46a6-849e-7ffd5cefee7f",
|
||||||
|
"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\", \"Total_hrc_transaction_amount\", \"Unique_country_codes\",\n",
|
||||||
|
" \"Segment\", \"Risk\", \"SAR_FLAG\"]\n",
|
||||||
|
" df = pd.DataFrame(row_list, columns = cols)\n",
|
||||||
|
" return df"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "dc94e713-7267-499a-897f-672209d563c0",
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<div>\n",
|
||||||
|
"<style scoped>\n",
|
||||||
|
" .dataframe tbody tr th:only-of-type {\n",
|
||||||
|
" vertical-align: middle;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe tbody tr th {\n",
|
||||||
|
" vertical-align: top;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe thead th {\n",
|
||||||
|
" text-align: right;\n",
|
||||||
|
" }\n",
|
||||||
|
"</style>\n",
|
||||||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||||||
|
" <thead>\n",
|
||||||
|
" <tr style=\"text-align: right;\">\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th>Focal_id</th>\n",
|
||||||
|
" <th>Total_hrc_transaction_amount</th>\n",
|
||||||
|
" <th>Unique_country_codes</th>\n",
|
||||||
|
" <th>Segment</th>\n",
|
||||||
|
" <th>Risk</th>\n",
|
||||||
|
" <th>SAR_FLAG</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>PN739187</td>\n",
|
||||||
|
" <td>5288386944.18</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>SME</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>PN573373</td>\n",
|
||||||
|
" <td>3984448017.83</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>Whole Sale Banking</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>Y</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>PN791113</td>\n",
|
||||||
|
" <td>2203599126.03</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>SME</td>\n",
|
||||||
|
" <td>High Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>PN572058</td>\n",
|
||||||
|
" <td>4424937298.78</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>Whole Sale Banking</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>PN375785</td>\n",
|
||||||
|
" <td>496146009.32</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>Whole Sale Banking</td>\n",
|
||||||
|
" <td>High Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>...</th>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10009</th>\n",
|
||||||
|
" <td>PN759572</td>\n",
|
||||||
|
" <td>42099733.92</td>\n",
|
||||||
|
" <td>IN</td>\n",
|
||||||
|
" <td>Ultra High NetWorth</td>\n",
|
||||||
|
" <td>Medium Risk</td>\n",
|
||||||
|
" <td>Y</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10010</th>\n",
|
||||||
|
" <td>PN147338</td>\n",
|
||||||
|
" <td>39374120.63</td>\n",
|
||||||
|
" <td>IN</td>\n",
|
||||||
|
" <td>Private Banking</td>\n",
|
||||||
|
" <td>Medium Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10011</th>\n",
|
||||||
|
" <td>PN632196</td>\n",
|
||||||
|
" <td>37892236.97</td>\n",
|
||||||
|
" <td>LK</td>\n",
|
||||||
|
" <td>Others</td>\n",
|
||||||
|
" <td>Medium Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10012</th>\n",
|
||||||
|
" <td>PN100406</td>\n",
|
||||||
|
" <td>35620658.59</td>\n",
|
||||||
|
" <td>IN</td>\n",
|
||||||
|
" <td>Others</td>\n",
|
||||||
|
" <td>High Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10013</th>\n",
|
||||||
|
" <td>PN452769</td>\n",
|
||||||
|
" <td>35792822.47</td>\n",
|
||||||
|
" <td>AM</td>\n",
|
||||||
|
" <td>Others</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"<p>10014 rows × 6 columns</p>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" Focal_id Total_hrc_transaction_amount Unique_country_codes \\\n",
|
||||||
|
"0 PN739187 5288386944.18 None \n",
|
||||||
|
"1 PN573373 3984448017.83 None \n",
|
||||||
|
"2 PN791113 2203599126.03 None \n",
|
||||||
|
"3 PN572058 4424937298.78 None \n",
|
||||||
|
"4 PN375785 496146009.32 None \n",
|
||||||
|
"... ... ... ... \n",
|
||||||
|
"10009 PN759572 42099733.92 IN \n",
|
||||||
|
"10010 PN147338 39374120.63 IN \n",
|
||||||
|
"10011 PN632196 37892236.97 LK \n",
|
||||||
|
"10012 PN100406 35620658.59 IN \n",
|
||||||
|
"10013 PN452769 35792822.47 AM \n",
|
||||||
|
"\n",
|
||||||
|
" Segment Risk SAR_FLAG \n",
|
||||||
|
"0 SME Low Risk N \n",
|
||||||
|
"1 Whole Sale Banking Low Risk Y \n",
|
||||||
|
"2 SME High Risk N \n",
|
||||||
|
"3 Whole Sale Banking Low Risk N \n",
|
||||||
|
"4 Whole Sale Banking High Risk N \n",
|
||||||
|
"... ... ... ... \n",
|
||||||
|
"10009 Ultra High NetWorth Medium Risk Y \n",
|
||||||
|
"10010 Private Banking Medium Risk N \n",
|
||||||
|
"10011 Others Medium Risk N \n",
|
||||||
|
"10012 Others High Risk N \n",
|
||||||
|
"10013 Others Low Risk N \n",
|
||||||
|
"\n",
|
||||||
|
"[10014 rows x 6 columns]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# sen = Scenario()\n",
|
||||||
|
"# sen.logic()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "7e585bbc-7baf-41a5-8f05-ca25c08e7ca8",
|
||||||
|
"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
|
||||||
|
}
|
||||||
401
main.ipynb
401
main.ipynb
@ -1,33 +1,368 @@
|
|||||||
{
|
{
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0",
|
"id": "e706cfb0-2234-4c4c-95d8-d1968f656aa0",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
"outputs": [],
|
"tags": []
|
||||||
"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"
|
},
|
||||||
}
|
"outputs": [],
|
||||||
],
|
"source": [
|
||||||
"metadata": {
|
"import pandas as pd"
|
||||||
"kernelspec": {
|
]
|
||||||
"display_name": "Python 3 (ipykernel)",
|
},
|
||||||
"language": "python",
|
{
|
||||||
"name": "python3"
|
"cell_type": "code",
|
||||||
},
|
"execution_count": 3,
|
||||||
"language_info": {
|
"id": "2edb58c0-33a2-4436-8128-05645af9990d",
|
||||||
"codemirror_mode": {
|
"metadata": {
|
||||||
"name": "ipython",
|
"tags": []
|
||||||
"version": 3
|
},
|
||||||
},
|
"outputs": [],
|
||||||
"file_extension": ".py",
|
"source": [
|
||||||
"mimetype": "text/x-python",
|
"from tms_data_interface import SQLQueryInterface\n",
|
||||||
"name": "python",
|
"seq = SQLQueryInterface(schema=\"transactionschema\")"
|
||||||
"nbconvert_exporter": "python",
|
]
|
||||||
"pygments_lexer": "ipython3",
|
},
|
||||||
"version": "3.8.13"
|
{
|
||||||
}
|
"cell_type": "code",
|
||||||
},
|
"execution_count": 4,
|
||||||
"nbformat": 4,
|
"id": "0ccc875a-6ff5-4741-9495-93c6871b1027",
|
||||||
"nbformat_minor": 5
|
"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": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"seq.execute_raw(\"show tables\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "01502887-b11f-46d2-9a52-e493df19d049",
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"query = \"\"\"\n",
|
||||||
|
" select final.CUSTOMER_NUMBER_main as Focal_id,\n",
|
||||||
|
" CAST(final.Total_hrc_transaction_amount AS DECIMAL(18, 2)) AS Total_hrc_transaction_amount,\n",
|
||||||
|
" final.Unique_country_codes,\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.Total_hrc_transaction_amount,\n",
|
||||||
|
" subquery.Unique_country_codes\n",
|
||||||
|
" from \n",
|
||||||
|
" (\n",
|
||||||
|
" select customer_number as CUSTOMER_NUMBER_1, \n",
|
||||||
|
" sum(transaction_amount) as Total_hrc_transaction_amount, \n",
|
||||||
|
" array_join(array_agg(DISTINCT benef_cntry_code), ',') AS unique_country_codes\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",
|
||||||
|
" 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",
|
||||||
|
"\"\"\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "c28e15b5-4b09-46a6-849e-7ffd5cefee7f",
|
||||||
|
"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\", \"Total_hrc_transaction_amount\", \"Unique_country_codes\",\n",
|
||||||
|
" \"Segment\", \"Risk\", \"SAR_FLAG\"]\n",
|
||||||
|
" df = pd.DataFrame(row_list, columns = cols)\n",
|
||||||
|
" return df"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "dc94e713-7267-499a-897f-672209d563c0",
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<div>\n",
|
||||||
|
"<style scoped>\n",
|
||||||
|
" .dataframe tbody tr th:only-of-type {\n",
|
||||||
|
" vertical-align: middle;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe tbody tr th {\n",
|
||||||
|
" vertical-align: top;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe thead th {\n",
|
||||||
|
" text-align: right;\n",
|
||||||
|
" }\n",
|
||||||
|
"</style>\n",
|
||||||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||||||
|
" <thead>\n",
|
||||||
|
" <tr style=\"text-align: right;\">\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th>Focal_id</th>\n",
|
||||||
|
" <th>Total_hrc_transaction_amount</th>\n",
|
||||||
|
" <th>Unique_country_codes</th>\n",
|
||||||
|
" <th>Segment</th>\n",
|
||||||
|
" <th>Risk</th>\n",
|
||||||
|
" <th>SAR_FLAG</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>PN739187</td>\n",
|
||||||
|
" <td>5288386944.18</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>SME</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>PN573373</td>\n",
|
||||||
|
" <td>3984448017.83</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>Whole Sale Banking</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>Y</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>PN791113</td>\n",
|
||||||
|
" <td>2203599126.03</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>SME</td>\n",
|
||||||
|
" <td>High Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>PN572058</td>\n",
|
||||||
|
" <td>4424937298.78</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>Whole Sale Banking</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>PN375785</td>\n",
|
||||||
|
" <td>496146009.32</td>\n",
|
||||||
|
" <td>None</td>\n",
|
||||||
|
" <td>Whole Sale Banking</td>\n",
|
||||||
|
" <td>High Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>...</th>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10009</th>\n",
|
||||||
|
" <td>PN759572</td>\n",
|
||||||
|
" <td>42099733.92</td>\n",
|
||||||
|
" <td>IN</td>\n",
|
||||||
|
" <td>Ultra High NetWorth</td>\n",
|
||||||
|
" <td>Medium Risk</td>\n",
|
||||||
|
" <td>Y</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10010</th>\n",
|
||||||
|
" <td>PN147338</td>\n",
|
||||||
|
" <td>39374120.63</td>\n",
|
||||||
|
" <td>IN</td>\n",
|
||||||
|
" <td>Private Banking</td>\n",
|
||||||
|
" <td>Medium Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10011</th>\n",
|
||||||
|
" <td>PN632196</td>\n",
|
||||||
|
" <td>37892236.97</td>\n",
|
||||||
|
" <td>LK</td>\n",
|
||||||
|
" <td>Others</td>\n",
|
||||||
|
" <td>Medium Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10012</th>\n",
|
||||||
|
" <td>PN100406</td>\n",
|
||||||
|
" <td>35620658.59</td>\n",
|
||||||
|
" <td>IN</td>\n",
|
||||||
|
" <td>Others</td>\n",
|
||||||
|
" <td>High Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10013</th>\n",
|
||||||
|
" <td>PN452769</td>\n",
|
||||||
|
" <td>35792822.47</td>\n",
|
||||||
|
" <td>AM</td>\n",
|
||||||
|
" <td>Others</td>\n",
|
||||||
|
" <td>Low Risk</td>\n",
|
||||||
|
" <td>N</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"<p>10014 rows × 6 columns</p>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" Focal_id Total_hrc_transaction_amount Unique_country_codes \\\n",
|
||||||
|
"0 PN739187 5288386944.18 None \n",
|
||||||
|
"1 PN573373 3984448017.83 None \n",
|
||||||
|
"2 PN791113 2203599126.03 None \n",
|
||||||
|
"3 PN572058 4424937298.78 None \n",
|
||||||
|
"4 PN375785 496146009.32 None \n",
|
||||||
|
"... ... ... ... \n",
|
||||||
|
"10009 PN759572 42099733.92 IN \n",
|
||||||
|
"10010 PN147338 39374120.63 IN \n",
|
||||||
|
"10011 PN632196 37892236.97 LK \n",
|
||||||
|
"10012 PN100406 35620658.59 IN \n",
|
||||||
|
"10013 PN452769 35792822.47 AM \n",
|
||||||
|
"\n",
|
||||||
|
" Segment Risk SAR_FLAG \n",
|
||||||
|
"0 SME Low Risk N \n",
|
||||||
|
"1 Whole Sale Banking Low Risk Y \n",
|
||||||
|
"2 SME High Risk N \n",
|
||||||
|
"3 Whole Sale Banking Low Risk N \n",
|
||||||
|
"4 Whole Sale Banking High Risk N \n",
|
||||||
|
"... ... ... ... \n",
|
||||||
|
"10009 Ultra High NetWorth Medium Risk Y \n",
|
||||||
|
"10010 Private Banking Medium Risk N \n",
|
||||||
|
"10011 Others Medium Risk N \n",
|
||||||
|
"10012 Others High Risk N \n",
|
||||||
|
"10013 Others Low Risk N \n",
|
||||||
|
"\n",
|
||||||
|
"[10014 rows x 6 columns]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# sen = Scenario()\n",
|
||||||
|
"# sen.logic()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "7e585bbc-7baf-41a5-8f05-ca25c08e7ca8",
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
|||||||
120
main.py
Normal file
120
main.py
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
# In[1]:
|
||||||
|
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
|
||||||
|
# In[3]:
|
||||||
|
|
||||||
|
|
||||||
|
from tms_data_interface import SQLQueryInterface
|
||||||
|
seq = SQLQueryInterface(schema="transactionschema")
|
||||||
|
|
||||||
|
|
||||||
|
# In[4]:
|
||||||
|
|
||||||
|
|
||||||
|
seq.execute_raw("show tables")
|
||||||
|
|
||||||
|
|
||||||
|
# In[5]:
|
||||||
|
|
||||||
|
|
||||||
|
query = """
|
||||||
|
select final.CUSTOMER_NUMBER_main as Focal_id,
|
||||||
|
CAST(final.Total_hrc_transaction_amount AS DECIMAL(18, 2)) AS Total_hrc_transaction_amount,
|
||||||
|
final.Unique_country_codes,
|
||||||
|
final.SEGMENT,
|
||||||
|
final.RISK,
|
||||||
|
final.SAR_FLAG
|
||||||
|
from
|
||||||
|
(
|
||||||
|
(
|
||||||
|
select subquery.CUSTOMER_NUMBER_1 as CUSTOMER_NUMBER_main,
|
||||||
|
subquery.Total_hrc_transaction_amount,
|
||||||
|
subquery.Unique_country_codes
|
||||||
|
from
|
||||||
|
(
|
||||||
|
select customer_number as CUSTOMER_NUMBER_1,
|
||||||
|
sum(transaction_amount) as Total_hrc_transaction_amount,
|
||||||
|
array_join(array_agg(DISTINCT benef_cntry_code), ',') AS unique_country_codes
|
||||||
|
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')
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# In[6]:
|
||||||
|
|
||||||
|
|
||||||
|
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", "Total_hrc_transaction_amount", "Unique_country_codes",
|
||||||
|
"Segment", "Risk", "SAR_FLAG"]
|
||||||
|
df = pd.DataFrame(row_list, columns = cols)
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
# In[7]:
|
||||||
|
|
||||||
|
|
||||||
|
# sen = Scenario()
|
||||||
|
# sen.logic()
|
||||||
|
|
||||||
|
|
||||||
|
# In[ ]:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user