Bcare Core Oracle Query

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

WITH ParentHierarchy (acct_id, parent_acct_id, cust_id, source_acct_nbr, acct_nbr,

state, level_1) AS (
SELECT
a.acct_id,
a.parent_acct_id,
a.cust_id,
a.acct_nbr AS source_acct_nbr,
a.acct_nbr,
a.state,
1 AS level_1
FROM
CC.ACCT a
WHERE
a.acct_nbr IN (SELECT customer_code FROM UMTQA.MIG_TMP_BACKOFFICE) -- Starting
point
UNION ALL
SELECT
ph.acct_id,
a.parent_acct_id,
a.cust_id,
ph.source_acct_nbr,
a.acct_nbr,
a.state,
ph.level_1 + 1
FROM
CC.ACCT a
INNER JOIN ParentHierarchy ph ON a.acct_id = ph.parent_acct_id
),
parent_hierarchy AS (
SELECT * FROM ParentHierarchy WHERE parent_acct_id IS NULL
),
paying_acct_identify (acct_id, parent_acct_id, cust_id, source_acct_nbr, acct_nbr,
state, bill_flag, level_1, paying_acct_nbr) AS (
SELECT
a.acct_id,
a.parent_acct_id,
a.cust_id,
a.acct_nbr AS source_acct_nbr,
a.acct_nbr,
a.state,
a.bill_flag,
1 AS level_1,
CASE WHEN a.bill_flag = 'Y' THEN a.acct_nbr ELSE NULL END AS paying_acct_nbr
FROM
CC.ACCT a
WHERE
a.acct_nbr IN (SELECT customer_code FROM UMTQA.MIG_TMP_BACKOFFICE)
UNION ALL
SELECT
ph.acct_id,
a.parent_acct_id,
a.cust_id,
ph.source_acct_nbr,
a.acct_nbr,
a.state,
a.bill_flag,
ph.level_1 + 1,
CASE WHEN a.bill_flag = 'Y' THEN a.acct_nbr ELSE ph.paying_acct_nbr END
FROM
CC.ACCT a
INNER JOIN paying_acct_identify ph ON a.acct_id = ph.parent_acct_id
WHERE ph.paying_acct_nbr IS NULL
),
paying_acct_final AS (
SELECT * FROM paying_acct_identify WHERE paying_acct_nbr IS NOT NULL
),
USER_PROFILE AS (
SELECT t1.*,
TO_DATE('2024-05-13', 'YYYY-MM-DD') - TRUNC(date_last_login) AS
days_last_login,
MONTHS_BETWEEN(TO_DATE('2024-05-02', 'YYYY-MM-DD'), date_last_login) AS
months_last_login,
CASE
WHEN status = '1' AND user_id = 'TERMINATED_ID' THEN 'TERMINATED'
WHEN status = '1' THEN 'ACTIVE'
WHEN status = '2' AND user_id = 'TERMINATED_ID' THEN 'TERMINATED'
WHEN status = '2' THEN 'INACTIVE'
WHEN status = '3' THEN 'BLOCKED'
WHEN status = '9' THEN 'TERMINATED'
END AS hexa_user_status
FROM UMTQA.MIG_TMP_SSO_USER_PROFILE t1
),
account_link AS (
SELECT
t2.user_id AS hexa_login_id,
t2.email AS sso_email,
t2.hexa_user_status,
t1.*,
t3.acct_id,
t3.cust_id,
CASE
WHEN t5.cust_type = 'A' THEN 'Personal'
WHEN t5.cust_type = 'C' THEN 'Corporate'
END AS customer_type,
t6.cert_nbr AS cert_number,
t7.cert_type_name,
t3.billing_cycle_type_id,
t3.state AS account_state,
t4.billing_cycle_type_name,
t3.parent_acct_id,
t3.bill_flag,
CASE WHEN t3.bill_flag = 'Y' THEN 'PR' ELSE 'NPR' END AS bill_flag_z
FROM UMTQA.MIG_TMP_BACKOFFICE t1
LEFT JOIN USER_PROFILE t2 ON t1.user_id = t2.user_id
LEFT JOIN CC.ACCT t3 ON t3.acct_nbr = t1.customer_code
LEFT JOIN cpc.cbec_billing_cycle_type t4 ON t4.billing_cycle_type_id =
t3.billing_cycle_type_id
LEFT JOIN CC.CUST t5 ON t5.cust_id = t3.cust_id
LEFT JOIN CC.CERT t6 ON t5.cert_id = t6.cert_id
LEFT JOIN CC.CERT_TYPE t7 ON t7.cert_type_id = t6.cert_type_id
),
temporary_table AS (
SELECT t1.*, t2.acct_nbr AS root_acct_nbr, t2.level_1 AS acct_nbr_level,
t3.paying_acct_nbr
FROM account_link t1
LEFT JOIN parent_hierarchy t2 ON t1.acct_id = t2.acct_id
LEFT JOIN paying_acct_final t3 ON t1.acct_id = t3.acct_id
),
filter_process as (
select * from temporary_table where hexa_user_status='ACTIVE' and account_state <>
'X'
),
UserAccounts AS (
SELECT
user_id,
root_acct_nbr,
COUNT(*) AS cnt
FROM
filter_process
GROUP BY
user_id,
root_acct_nbr
),
UserClassification AS (
SELECT
user_id,
COUNT(DISTINCT root_acct_nbr) AS num_acct_nbrs, -- Counts distinct account
numbers per user
MAX(cnt) AS max_count -- Maximum count of any single account number
FROM
UserAccounts
GROUP BY
user_id
),
EmailDuplicate AS (
SELECT
sso_email,
COUNT(DISTINCT user_id) AS user_count
FROM
filter_process
GROUP BY
sso_email
HAVING
COUNT(DISTINCT user_id) > 1
),
UserEmails AS (
SELECT DISTINCT
user_id,
sso_email
FROM
filter_process
),
analysis_final as (
SELECT
UE.user_id,
UE.sso_email AS email,
CASE
WHEN UC.num_acct_nbrs = 1 AND UC.max_count = 1 THEN 'ONE_ACCOUNT'
WHEN UC.num_acct_nbrs = 1 AND UC.max_count > 1 THEN
'MULTI_ACCOUNT_SAME_BRN'
ELSE 'MULTI_ACCOUNT_DIFFERENT_BRN'
END AS Account_Type,
CASE
WHEN ED.sso_email IS NOT NULL THEN 'SAME_EMAIL_MULTIPLE_USER'
ELSE NULL
END AS Email_Tag
FROM
UserClassification UC
LEFT JOIN
UserEmails UE ON UC.user_id = UE.user_id
LEFT JOIN
EmailDuplicate ED ON UE.sso_email = ED.sso_email
GROUP BY
UE.user_id, UE.sso_email, UC.num_acct_nbrs, UC.max_count, ED.sso_email
) select t1.*,
case
when t2.Account_Type is not null then t2.account_type
when t2.account_type is null then 'NOT_MIGRATE'
end ACCOUNT_CLASSFICATION,t2.Email_Tag,t2.email as TAGGED_EMAIL from
temporary_table t1
left join analysis_final t2 on t1.user_id =t2.user_id

You might also like