Change of transformation order for stg_AGENTS

This commit is contained in:
Anna Saiapina 2024-07-24 08:01:21 +00:00 committed by GCP Dataform
parent be7bc10be4
commit 75298e24d5

View File

@ -9,90 +9,81 @@ config {
description: "All fields that are connected to the AGENT" description: "All fields that are connected to the AGENT"
} }
with last_agent_digital as ( with agent_digital as (
select AGENT_ID, select AGENT_ID,
AGENT_NAME, 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, AGENT_GROUP,
ROW_NUMBER () case
OVER (partition by AGENT_ID order by timestamp desc) row_n 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,
timestamp
from ${ref("stg_digital")} from ${ref("stg_digital")}
), ),
last_agent_calls as ( agent_calls as (
select AGENT_ID, select AGENT_ID,
AGENT_NAME, 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, AGENT_GROUP,
ROW_NUMBER () case
OVER (partition by AGENT_ID order by timestamp desc) row_n 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,
timestamp
from ${ref("stg_calls")} 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
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_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 ( all_agents as (
select * from agent_digital_dedup select * from agent_digital
union distinct union distinct
select * from agent_calls_dedup select * from agent_calls
),
all_agents_dedup as(
select *,
ROW_NUMBER ()
OVER (partition by AGENT_ID order by timestamp desc) row_n
from all_agents
) )
select all_agents.AGENT_ID, select all_agents_dedup.AGENT_ID,
all_agents.AGENT_NAME, all_agents_dedup.AGENT_NAME,
all_agents.LANGUAGES, all_agents_dedup.LANGUAGES,
ml.CODE, ml.CODE,
all_agents.AGENT_GROUP, all_agents_dedup.AGENT_GROUP,
ag.AGENT_GROUP_ID ag.AGENT_GROUP_ID
from all_agents from all_agents_dedup
left join ${ref("stg_MAPPING_AGENT_GROUP")} ag left join ${ref("stg_MAPPING_AGENT_GROUP")} ag
on all_agents.AGENT_GROUP = ag.AGENT_GROUP on all_agents_dedup.AGENT_GROUP = ag.AGENT_GROUP
left join ${ref("stg_MAPPING_LANGUAGES")} ml left join ${ref("stg_MAPPING_LANGUAGES")} ml
on upper(all_agents.LANGUAGES) = upper(ml.LANGUAGE) on upper(all_agents_dedup.LANGUAGES) = upper(ml.LANGUAGE)
order by agent_id where row_n = 1