75/main.ipynb
2024-10-09 03:19:56 +00:00

369 lines
12 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"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
}