From ea7475b57c992ec30bcc4638569e7ee8813d3e7c Mon Sep 17 00:00:00 2001 From: user_client2024 Date: Wed, 9 Oct 2024 03:33:57 +0000 Subject: [PATCH] System save at 09/10/2024 09:03 by user_client2024 --- .ipynb_checkpoints/main-checkpoint.ipynb | 197 +++++++++++++++++++ main.ipynb | 230 +++++++++++++++++++---- main.py | 118 ++++++++++++ 3 files changed, 512 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..f073ae1 --- /dev/null +++ b/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,197 @@ +{ + "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": 2, + "id": "f35b1262-3c20-44a6-bbd3-2679a15551e6", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from tms_data_interface import SQLQueryInterface\n", + "seq = SQLQueryInterface(schema=\"transactionschema\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e52124e8-4f62-449d-8852-1e04f8c01ecc", + "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": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "seq.execute_raw(\"show tables\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "dda35e8d-8997-42d4-a472-844c208d0f49", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "query = \"\"\"\n", + " select final.CUSTOMER_NUMBER_main as Focal_id,\n", + " final.Credit_transaction_amount,\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", + " from \n", + " (\n", + " (\n", + " select customer_number as CUSTOMER_NUMBER_1, \n", + " sum(transaction_amount) as Credit_transaction_amount\n", + " from \n", + " (\n", + " select * \n", + " from {trans_data} as trans_table \n", + " left join {acc_data} as acc_table\n", + " on trans_table.benef_account_number = acc_table.account_number\n", + " where trans_table.transaction_desc = 'WIRE RELATED TRANSACTION'\n", + " )\n", + " where account_number not in ('None')\n", + " group by 1\n", + " ) credit\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" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "fb0405fe-cd10-4da1-9f06-fe52cff942b4", + "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_Wire_Deposit_Amt\",\n", + " \"Segment\", \"Risk\", \"SAR_FLAG\"]\n", + " df = pd.DataFrame(row_list, columns = cols)\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ddc11b42-6cbb-419b-9e26-73e7606e18a6", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# sen = Scenario()\n", + "# sen.logic()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "157c4e46-2cff-4f6f-acba-faf4d73538cf", + "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..f073ae1 100644 --- a/main.ipynb +++ b/main.ipynb @@ -1,33 +1,197 @@ -{ - "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": 2, + "id": "f35b1262-3c20-44a6-bbd3-2679a15551e6", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from tms_data_interface import SQLQueryInterface\n", + "seq = SQLQueryInterface(schema=\"transactionschema\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e52124e8-4f62-449d-8852-1e04f8c01ecc", + "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": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "seq.execute_raw(\"show tables\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "dda35e8d-8997-42d4-a472-844c208d0f49", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "query = \"\"\"\n", + " select final.CUSTOMER_NUMBER_main as Focal_id,\n", + " final.Credit_transaction_amount,\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", + " from \n", + " (\n", + " (\n", + " select customer_number as CUSTOMER_NUMBER_1, \n", + " sum(transaction_amount) as Credit_transaction_amount\n", + " from \n", + " (\n", + " select * \n", + " from {trans_data} as trans_table \n", + " left join {acc_data} as acc_table\n", + " on trans_table.benef_account_number = acc_table.account_number\n", + " where trans_table.transaction_desc = 'WIRE RELATED TRANSACTION'\n", + " )\n", + " where account_number not in ('None')\n", + " group by 1\n", + " ) credit\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" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "fb0405fe-cd10-4da1-9f06-fe52cff942b4", + "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_Wire_Deposit_Amt\",\n", + " \"Segment\", \"Risk\", \"SAR_FLAG\"]\n", + " df = pd.DataFrame(row_list, columns = cols)\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ddc11b42-6cbb-419b-9e26-73e7606e18a6", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# sen = Scenario()\n", + "# sen.logic()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "157c4e46-2cff-4f6f-acba-faf4d73538cf", + "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..8f31a3b --- /dev/null +++ b/main.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[1]: + + +import pandas as pd + + +# In[2]: + + +from tms_data_interface import SQLQueryInterface +seq = SQLQueryInterface(schema="transactionschema") + + +# In[3]: + + +seq.execute_raw("show tables") + + +# In[4]: + + +query = """ + select final.CUSTOMER_NUMBER_main as Focal_id, + final.Credit_transaction_amount, + final.SEGMENT, + final.RISK, + final.SAR_FLAG + from + ( + ( + select subquery.CUSTOMER_NUMBER_1 as CUSTOMER_NUMBER_main, + subquery.Credit_transaction_amount + from + ( + ( + select customer_number as CUSTOMER_NUMBER_1, + sum(transaction_amount) as Credit_transaction_amount + 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 trans_table.transaction_desc = 'WIRE RELATED TRANSACTION' + ) + where account_number not in ('None') + group by 1 + ) credit + ) 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[5]: + + +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_Wire_Deposit_Amt", + "Segment", "Risk", "SAR_FLAG"] + df = pd.DataFrame(row_list, columns = cols) + return df + + +# In[7]: + + +# sen = Scenario() +# sen.logic() + + +# In[ ]: + + + +