Update script with agents deduplication

This commit is contained in:
Anna Saiapina 2024-07-16 11:03:00 +00:00 committed by GCP Dataform
parent 2e068f0859
commit 51cd892506

View File

@ -9,48 +9,90 @@ config {
description: "All fields that are connected to the AGENT" description: "All fields that are connected to the AGENT"
} }
with LANGS as ( with last_agent_digital as (
select AGENT_ID,
AGENT_NAME,
AGENT_GROUP,
ROW_NUMBER ()
OVER (partition by AGENT_ID order by timestamp desc) row_n
from ${ref("stg_digital")}
),
last_agent_calls as (
select AGENT_ID,
AGENT_NAME,
AGENT_GROUP,
ROW_NUMBER ()
OVER (partition by AGENT_ID order by timestamp desc) row_n
from ${ref("stg_calls")}
),
agent_digital_dedup as (
select AGENT_ID, select AGENT_ID,
AGENT_NAME, case
AGENT_GROUP, when strpos(AGENT_NAME, '(') > 0 and strpos(AGENT_NAME, ') ') = 0
case then left(agent_name, strpos(AGENT_NAME, '(')-1)
when strpos(AGENT_NAME, '-- ') > 0 when strpos(AGENT_NAME, ' -') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 3) then left(agent_name, strpos(AGENT_NAME, ' -')-1)
when strpos(AGENT_NAME, '--') > 0 when strpos(AGENT_NAME, '-') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 2) then left(agent_name, strpos(AGENT_NAME, '-')-1)
when strpos(AGENT_NAME, '- ') > 0 else AGENT_NAME
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 2) end AGENT_NAME,
when strpos(AGENT_NAME, '-') > 0 AGENT_GROUP,
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 1) case
else null when strpos(AGENT_NAME, '-- ') > 0
end AS LANGUAGES then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 3))
from ${ref("stg_digital")} when strpos(AGENT_NAME, '--') > 0
union distinct ( then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 2))
select AGENT_ID, when strpos(AGENT_NAME, '- ') > 0
AGENT_NAME, then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 2))
AGENT_GROUP, when strpos(AGENT_NAME, '-') > 0
case then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 1))
when strpos(AGENT_NAME, '-- ') > 0 else null
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 3) end AS LANGUAGES
when strpos(AGENT_NAME, '--') > 0 from last_agent_digital
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 2) where row_n = 1
when strpos(AGENT_NAME, '- ') > 0 ),
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 2) agent_calls_dedup as (
when strpos(AGENT_NAME, '-') > 0 select AGENT_ID,
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 1) case
else null when strpos(AGENT_NAME, '(') > 0 and strpos(AGENT_NAME, ') ') = 0
end AS LANGUAGES then left(agent_name, strpos(AGENT_NAME, '(')-1)
from ${ref("stg_calls")}) when strpos(AGENT_NAME, ' -') > 0
then left(agent_name, strpos(AGENT_NAME, ' -')-1)
when strpos(AGENT_NAME, '-') > 0
then left(agent_name, strpos(AGENT_NAME, '-')-1)
else AGENT_NAME
end AGENT_NAME,
AGENT_GROUP,
case
when strpos(AGENT_NAME, '-- ') > 0
then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 3))
when strpos(AGENT_NAME, '--') > 0
then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 2))
when strpos(AGENT_NAME, '- ') > 0
then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 2))
when strpos(AGENT_NAME, '-') > 0
then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 1))
else null
end AS LANGUAGES
from last_agent_calls
where row_n = 1
),
all_agents as (
select * from agent_digital_dedup
union distinct
select * from agent_calls_dedup
) )
select distinct l.AGENT_ID, select all_agents.AGENT_ID,
l.AGENT_NAME, all_agents.AGENT_NAME,
l.LANGUAGES, all_agents.LANGUAGES,
ml.CODE, ml.CODE,
l.AGENT_GROUP, all_agents.AGENT_GROUP,
ag.AGENT_GROUP_ID ag.AGENT_GROUP_ID
from LANGS l from all_agents
left join ${ref("stg_MAPPING_AGENT_GROUP")} ag left join ${ref("stg_MAPPING_AGENT_GROUP")} ag
on l.AGENT_GROUP = ag.AGENT_GROUP on all_agents.AGENT_GROUP = ag.AGENT_GROUP
left join ${ref("stg_MAPPING_LANGUAGES")} ml left join ${ref("stg_MAPPING_LANGUAGES")} ml
on upper(l.LANGUAGES) = upper(ml.LANGUAGE) on upper(all_agents.LANGUAGES) = upper(ml.LANGUAGE)
order by agent_id