generated from user_client2024/78
190 lines
5.7 KiB
Python
190 lines
5.7 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
# In[1]:
|
|
|
|
|
|
# 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[2]:
|
|
|
|
|
|
import pandas as pd
|
|
|
|
query = """
|
|
SELECT
|
|
t.transaction_key,
|
|
t.transaction_date,
|
|
t.transaction_amount,
|
|
t.transaction_desc,
|
|
t.benef_account_number,
|
|
|
|
-- Account data
|
|
a.account_number,
|
|
a.customer_number AS acc_customer_number,
|
|
a.account_type,
|
|
a.branch_code,
|
|
|
|
-- Party data
|
|
p.customer_number AS party_customer_number,
|
|
p.customer_name,
|
|
p.date_of_birth,
|
|
p.nationality,
|
|
p.business_segment,
|
|
CASE
|
|
WHEN p.risk_classification = 1 THEN 'Low Risk'
|
|
WHEN p.risk_classification = 2 THEN 'Medium Risk'
|
|
WHEN p.risk_classification = 3 THEN 'High Risk'
|
|
ELSE 'Unknown Risk'
|
|
END AS risk_level,
|
|
|
|
-- Alert data
|
|
COALESCE(al.sar_flag, 'N') AS sar_flag
|
|
|
|
FROM {trans_data} t
|
|
|
|
-- Join with account data on beneficiary account
|
|
LEFT JOIN {acc_data} a
|
|
ON t.benef_account_number = a.account_number
|
|
|
|
-- Join with party/customer data using account's customer number
|
|
LEFT JOIN {cust_data} p
|
|
ON a.customer_number = p.customer_number
|
|
|
|
-- Join with alert data using party's customer number
|
|
LEFT JOIN {alert_data} al
|
|
ON p.customer_number = al.customer_number
|
|
|
|
WHERE a.account_number IS NOT NULL
|
|
limit 100
|
|
"""
|
|
|
|
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")
|
|
)
|
|
"Focal_id", "Cash_deposit_total", "Cash_deposit_count",
|
|
"Segment", "Risk", "SAR_FLAG"
|
|
cols = [
|
|
"Focal_id",
|
|
"transaction_date",
|
|
"transaction_amount",
|
|
"transaction_desc",
|
|
"benef_account_number",
|
|
"account_number",
|
|
"acc_customer_number",
|
|
"account_type",
|
|
"branch_code",
|
|
"party_customer_number",
|
|
"customer_name",
|
|
"date_of_birth",
|
|
"nationality",
|
|
"Segment",
|
|
"Risk",
|
|
"SAR_FLAG"
|
|
]
|
|
df = pd.DataFrame(row_list, columns = cols)
|
|
return df
|
|
|
|
|
|
# In[4]:
|
|
|
|
|
|
# sen = Scenario()
|
|
# sen.logic()
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|