diff --git a/definitions/staging/stg_AGENT.sqlx b/definitions/staging/stg_AGENT.sqlx index 8c5ac9d..c7fec84 100644 --- a/definitions/staging/stg_AGENT.sqlx +++ b/definitions/staging/stg_AGENT.sqlx @@ -9,24 +9,7 @@ config { description: "All fields that are connected to the AGENT" } -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 ( +with agent_digital as ( select AGENT_ID, case when strpos(AGENT_NAME, '(') > 0 and strpos(AGENT_NAME, ') ') = 0 @@ -48,11 +31,11 @@ agent_digital_dedup as ( 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 + end AS LANGUAGES, + timestamp + from ${ref("stg_digital")} ), -agent_calls_dedup as ( +agent_calls as ( select AGENT_ID, case when strpos(AGENT_NAME, '(') > 0 and strpos(AGENT_NAME, ') ') = 0 @@ -74,25 +57,33 @@ agent_calls_dedup as ( 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 + end AS LANGUAGES, + timestamp + from ${ref("stg_calls")} ), all_agents as ( - select * from agent_digital_dedup + select * from agent_digital 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, - all_agents.AGENT_NAME, - all_agents.LANGUAGES, +select all_agents_dedup.AGENT_ID, + all_agents_dedup.AGENT_NAME, + all_agents_dedup.LANGUAGES, ml.CODE, - all_agents.AGENT_GROUP, + all_agents_dedup.AGENT_GROUP, ag.AGENT_GROUP_ID -from all_agents +from all_agents_dedup 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 -on upper(all_agents.LANGUAGES) = upper(ml.LANGUAGE) -order by agent_id \ No newline at end of file +on upper(all_agents_dedup.LANGUAGES) = upper(ml.LANGUAGE) +where row_n = 1 \ No newline at end of file