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, select AGENT_ID,
AGENT_NAME, AGENT_NAME,
AGENT_GROUP, AGENT_GROUP,
case ROW_NUMBER ()
when strpos(AGENT_NAME, '-- ') > 0 OVER (partition by AGENT_ID order by timestamp desc) row_n
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 3)
when strpos(AGENT_NAME, '--') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 2)
when strpos(AGENT_NAME, '- ') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 2)
when strpos(AGENT_NAME, '-') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 1)
else null
end AS LANGUAGES
from ${ref("stg_digital")} from ${ref("stg_digital")}
union distinct ( ),
last_agent_calls as (
select AGENT_ID, select AGENT_ID,
AGENT_NAME, AGENT_NAME,
AGENT_GROUP, 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,
case
when strpos(AGENT_NAME, '(') > 0 and 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)
when strpos(AGENT_NAME, '-') > 0
then left(agent_name, strpos(AGENT_NAME, '-')-1)
else AGENT_NAME
end AGENT_NAME,
AGENT_GROUP,
case case
when strpos(AGENT_NAME, '-- ') > 0 when strpos(AGENT_NAME, '-- ') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 3) then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 3))
when strpos(AGENT_NAME, '--') > 0 when strpos(AGENT_NAME, '--') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 2) then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '--') + 2))
when strpos(AGENT_NAME, '- ') > 0 when strpos(AGENT_NAME, '- ') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 2) then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 2))
when strpos(AGENT_NAME, '-') > 0 when strpos(AGENT_NAME, '-') > 0
then substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 1) then upper(substr(AGENT_NAME, strpos(AGENT_NAME, '-') + 1))
else null else null
end AS LANGUAGES end AS LANGUAGES
from ${ref("stg_calls")}) from last_agent_digital
where row_n = 1
),
agent_calls_dedup as (
select AGENT_ID,
case
when strpos(AGENT_NAME, '(') > 0 and 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)
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