You are on page 1of 7

and t2.

inbound_type = 2 -- OB

-- and t2.ctime-t1.ctime > 0

and t1.grass_region = '${UDF_REGION}'

-- and t1.channel_type = 3

and t1.case_id != 0

and case_tab.channel_type in (3) -- EMAIL

group by t1.case_id

having date(from_unixtime(MIN(t2.ctime), '${MY_TZ}')) = ${L1D_Date}

and MIN(t2.ctime) - MIN(t1.ctime) < 60*60*24*7 --limit for data mart look back period to find
reply

and MIN(t2.ctime) - MIN(t1.ctime) > 0 --to exclude cases where OB came first (else
response time will be neg)

),

reply_time_metric_webform as

select distinct t1.case_id

,MIN(t1.ctime) as incoming_time

,MIN(t2.ctime) as first_reply_sent_time

from chatbot_data.shopee_seller_cs_case_db__enquiry_tab__my_continuous_s0_live as t1

inner join chatbot_data.shopee_seller_cs_case_db__enquiry_tab__my_continuous_s0_live as t2

on t1.case_id = t2.case_id

and t1.grass_region = t2.grass_region

inner join chatbot_data.shopee_seller_cs_case_db__case_tab__my_continuous_s0_live as


case_tab

on case_tab.case_id = t1.case_id

and case_tab.grass_region = t1.grass_region


where t1.inbound_type = 1 -- IB

-- and t1.is_first_in_case = 1

and t2.inbound_type = 2 -- OB

-- and t2.ctime-t1.ctime > 0

and t1.grass_region = '${UDF_REGION}'

-- and t1.channel_type = 3

and t1.case_id != 0

and case_tab.channel_type in (20, 21) -- GEN AND SPECIAL WEBFORM

group by t1.case_id

having date(from_unixtime(MIN(t2.ctime), '${MY_TZ}')) = ${L1D_Date}

and MIN(t2.ctime) - MIN(t1.ctime) < 60*60*24*7 --limit for data mart look back period to find
reply

and MIN(t2.ctime) - MIN(t1.ctime) > 0 --to exclude cases where OB came first (else
response time will be neg)

),

reply_time_metric as

select * from reply_time_metric_email

union

select * from reply_time_metric_webform

),

special_form_tab as

select enquiry_tab.case_id

,COALESCE(COUNT(CASE WHEN form_tab.form_type = 2 then 1 ELSE NULL END), 0)


special_form_count
from chatbot_data.shopee_seller_cs_form_db__form_submission_tab__my_continuous_s0_live
as form_submission_tab

-- to get form_type --

left join chatbot_data.shopee_seller_cs_channel_form_db__form_tab__my_continuous_s0_live


as form_tab

on form_tab.id = form_submission_tab.form_id

-- to get case_id --

left join chatbot_data.shopee_seller_cs_case_db__enquiry_tab__my_continuous_s0_live as


enquiry_tab

on enquiry_tab.channel_event_id = cast(form_submission_tab.id as varchar)

where form_submission_tab.grass_region = '${UDF_REGION}'

and form_tab.region = '${UDF_REGION}'

and enquiry_tab.grass_region = '${UDF_REGION}'

group by enquiry_tab.case_id

having enquiry_tab.case_id in (select case_id from reply_time_metric)

),

case_table as

select case_tab.case_id

,case_tab.user_id as cs_user_id

,case_related_attr_tab.user_type

,case_tab.channel_type

,case case_tab.channel_type when 1 then 'Chat'

when 3 then 'Email'

when 6 then 'Call'

when 7 then 'Facebook_Private'


when 8 then 'Facebook_Public'

when 9 then 'Twitter_Private'

when 10 then 'Twitter_Public'

when 11 then 'Instagram_Private'

when 12 then 'Instagram_Public'

when 13 then 'Google_Play_Public'

when 14 then 'App_Store_Public'

when 15 then 'WhatsApp_Private'

when 16 then 'Reclame_Aqui_Public'

when 17 then 'OutboundEmail'

when 18 then 'OutboundCall'

when 19 then 'OutboundSocialMedia'

when 20 then 'WebForm_General'

when 21 then 'WebForm_Special'

when 22 then 'InAppCaseTracker'

else cast(case_tab.channel_type as varchar) end as channel

,case_tab.owner_id

,case case_tab.owner_type when 1 then 'agent'

when 2 then 'queue'

else cast(case_tab.owner_type as varchar) end as owner_type

,case_tab.enquiry_config_id

,reason_view.l1_name

,reason_view.l2_name

,reason_view.l3_name

from chatbot_data.shopee_seller_cs_case_db__case_tab__my_continuous_s0_live as case_tab

-- to get user_type --

left join
chatbot_data.shopee_seller_cs_case_db__case_related_attr_tab__my_continuous_s0_live as
case_related_attr_tab

on case_tab.case_id = case_related_attr_tab.case_id
and case_tab.grass_region = case_related_attr_tab.grass_region

-- to get RCs --

left join regops_cs.dim_reason_view_my_code_mapping_staging_di_live as reason_view

on case_tab.enquiry_config_id = reason_view.l3_id

where case_tab.case_id in (select case_id from reply_time_metric)

and case_tab.grass_region = '${UDF_REGION}'

),

-- IN request: add agent details --

agent_admin as

select account_id

,agent_name

,agent_email

,agent_owner_team

,company_name

,reporting_manager_email

from regops_cs.dim_agent_my_agent_admin_snapshot_di_live

where report_date = ${L1D_Date}

and grass_region = '${UDF_REGION}'

),

queue_table as

select distinct id as queue_id

,queue_name

,owner_id as queue_owner_id

from chatbot_data.shopee_seller_cs_routing_db__queue_tab__my_continuous_s0_live
where grass_region = '${UDF_REGION}'

and status_flag = 1

and id in (select owner_id from case_table)

),

queue_owner as

select distinct agent_type_id as queue_owner_id

,agent_type_name as queue_owner_team

from chatbot_data.shopee_seller_cs_account_db__agent_type_tab__my_continuous_s0_live

where grass_region = '${UDF_REGION}'

and status_flag = 1

and agent_type_id in (select queue_owner_id from queue_table)

),

-- ID request: add case initial owner details --

init_owner as

select case_id

,initial_owner_id

from regops_cs.dwd_rep_my_cs_kpi_shopee_case_initial_owner_di_live

where report_date = ${L1D_Date}

and grass_region = '${UDF_REGION}'

and case_id in (select case_id from case_table)

),

initial_owner as

select case_table.case_id

-- if no initial owner, initial owner = current owner --

,COALESCE(cast(init_owner.initial_owner_id as varchar), cast(case_table.owner_id as


varchar)) as initial_owner_id
from case_table

left join init_owner

on case_table.case_id = init_owner.case_id

),

initial_owner_details as

select case_id

,cast(initial_owner_id as bigint) as initial_owner_id

,agent_name as initial_owner_agent_name

,agent_email as initial_owner_agent_email

,agent_owner_team as initial_owner_agent_owner_team

from initial_owner

left join agent_admin

on initial_owner.initial_owner_id = cast(agent_admin.account_id as varchar)

select

reply_table.case_id

,reply_table.incoming_time

,cast(from_unixtime(reply_table.incoming_time, '${MY_TZ}') as timestamp) as


incoming_time_datetime

,reply_table.first_reply_sent_time

,cast(from_unixtime(reply_table.first_reply_sent_time, '${MY_TZ}') as timestamp) as


first_reply_sent_time_datetime

,(reply_table.first_reply_sent_time - reply_table.incoming_time) as first_response_time

You might also like